繁体   English   中英

在NodeJS上使用Regenerator运行时

[英]Using regenerator runtime with NodeJS

我试图require regeneratorRuntime对象,以便它在全球范围内可用,因此我的Node.js代码可与我的应用程序中任何位置的任何异步函数/生成器babel转译一起使用。 regenerator是通过npm install regenerator

我的问题是,为什么这段代码

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);
}

不能按预期工作,导致记录未定义,同时将第一行替换为

global.regenratorRuntime = require('regenerator/runtime');

导致预期的结果。

在运行时文件中查看此代码

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 ,但是我想了解为什么这样做是必要的。 似乎Node从require语句执行的代码的行为可能与我假设的有所不同。

作为辅助问题,任何人都可以指出self检查要检查的内容吗?

它确实

global.regeneratorRuntime
//          ^

global.regenratorRuntime

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM