regenerator
用来转换 generator 和 async 函数,这个包实现了一个功能齐全的源代码转换,它采用了ECMAScripts 2015 or ECMA 2015的的异步迭代器提案中的 generators/yield 语法并高效的产输出行为相同的ES5。
功能举例如下:
// 转换前
export async function fn() {
Promise.resolve()
console.log(123);
}
🔽🔽🔽
🔽🔽🔽
// 转换后
export function fn() {
return regeneratorRuntime.async(function fn$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
Promise.resolve();
console.log(123);
case 2:
case "end":
return _context.stop();
}
}
}, null, null, null, Promise);
}
regenerator-runtime
regenerator 包编译 generator 和 async 函数的独立运行时(即编译后代码的主要功能实现)。由于该包导出的文件是runtime.js,因此也可以写成regenerator-runtime/runtime。
// 查看regenerator-runtime 暴露的内容
console.log("regeneratorRuntime:", require("regenerator-runtime"));
// 打印结果如下
// 可发现它提供了regenerator 执行编译后的方法,如上文出现的regeneratorRuntime.async()
regeneratorRuntime: {
wrap: [Function: wrap],
isGeneratorFunction: [Function],
mark: [Function],
awrap: [Function],
AsyncIterator: [Function: AsyncIterator],
async: [Function],
keys: [Function],
values: [Function: values]
}