generator - 为什么生成器的 es5 转译代码有一段时间(1)
问题描述
与 babel repl 一样,如果我们有类似的 es6 代码:
function* test() {
let a = yield 1
let b = yield 2
}
它将被编译成
"use strict";
var _marked =
/*#__PURE__*/
regeneratorRuntime.mark(test);
function test() {
var a, b;
return regeneratorRuntime.wrap(function test$(_context) {
while (1) {
switch ((_context.prev = _context.next)) {
case 0:
_context.next = 2;
return 1;
case 2:
a = _context.sent;
_context.next = 5;
return 2;
case 5:
b = _context.sent;
case 6:
case "end":
return _context.stop();
}
}
}, _marked);
}
只是无法理解生成代码中while(1)的目的是什么(如在case部分中,它使用return,它会跳出while(1)并且如果while(1)生效,整个 Javascript 线程都将被占用,无法进行其他操作)
解决方案
推荐阅读
- javascript - 重置对数组的更改
- algorithm - 如何获得 N 个项目的所有可能组合,分为 M 个项目的集合,并且同一集合中两个项目的出现值对于每个项目都相同?
- android - 修复 ionic cordova google-services/firebase 构建错误
- java - 我想使用java将json文件发送到elasticsearch
- css - 如何让 ui-select 边框 CSS 成为焦点
- odbc - 在 R Studio Server 中找不到 ODBC SQL 服务器
- prolog - Prolog用非地面文字实现否定的语义是什么
- c++ - 将 std::filesystem 头文件添加到我的程序时出现编译错误
- azure - Azure AD Premium 企业应用程序许可
- java - 在任务栏上固定使用 Netbeans 创建的 Java 程序?