Javascript 将再生器运行时与NodeJS一起使用
我试图Javascript 将再生器运行时与NodeJS一起使用,javascript,node.js,Javascript,Node.js,我试图要求regeneratorRuntime对象,使其全局可用,因此我的Node.js代码将在我的应用程序中的任何位置与任何异步函数/生成器babel Transfiles一起工作再生器是通过npm安装的npm安装再生器 我的问题是,为什么这个代码 require('regenerator/runtime'); console.log(typeof regenratorRuntime); if (typeof regenratorRuntime === 'object'){ cons
要求
regeneratorRuntime对象,使其全局可用,因此我的Node.js代码将在我的应用程序中的任何位置与任何异步函数/生成器babel Transfiles一起工作<代码>再生器是通过npm安装的npm安装再生器
我的问题是,为什么这个代码
require('regenerator/runtime');
console.log(typeof regenratorRuntime);
if (typeof regenratorRuntime === 'object'){
console.log(typeof regenratorRuntime.wrap);
console.log(typeof regenratorRuntime.awrap);
console.log(typeof regenratorRuntime.async);
console.log(typeof regenratorRuntime.mark);
}
runtime = global.regeneratorRuntime = inModule ? module.exports : {};
未按预期工作,导致未定义的日志记录,而将第一行替换为
global.regenratorRuntime = require('regenerator/runtime');
导致预期的结果
在运行时文件中,我看到了以下代码
require('regenerator/runtime');
console.log(typeof regenratorRuntime);
if (typeof regenratorRuntime === 'object'){
console.log(typeof regenratorRuntime.wrap);
console.log(typeof regenratorRuntime.awrap);
console.log(typeof regenratorRuntime.async);
console.log(typeof regenratorRuntime.mark);
}
runtime = global.regeneratorRuntime = inModule ? module.exports : {};
在IIFE中,此表达式作为global
(
// Among the various tricks for obtaining a reference to the global
// object, this seems to be the most reliable technique that does not
// use indirect eval (which violates Content Security Policy).
typeof global === "object" ? global :
typeof window === "object" ? window :
typeof self === "object" ? self : this
);
我希望在全局对象上正确设置regenratorRuntime
我不介意手动设置global.regenratorRuntime
,但我想了解为什么需要这样做。似乎代码节点从require
语句执行时的行为可能与我假设的不同
作为辅助事项,是否有人能指出自检检查的目的是什么?它确实设置了
global.regeneratorRuntime
// ^
不是
它真的定了
global.regeneratorRuntime
// ^
不是
它真的定了
global.regeneratorRuntime
// ^
不是
它真的定了
global.regeneratorRuntime
// ^
不是
self
确实存在于工作人员中,其中window
不self
存在于工作人员中,其中window
不self
存在于工作人员中,其中window
不太感谢您。看来十年的经验还不足以阻止这种愚蠢行为:-非常感谢。看来十年的经验还不足以阻止这种愚蠢行为:-非常感谢。看来十年的经验还不足以阻止这种愚蠢行为:-非常感谢。十年的经验似乎不足以阻止这种愚蠢行为:-|