简体   繁体   English

在NodeJS上使用Regenerator运行时

[英]Using regenerator runtime with NodeJS

I'm trying to require the regeneratorRuntime object so that it's globally available, so my Node.js code will work with any async functions / generators babel transpiles for me anywhere in my application. 我试图require regeneratorRuntime对象,以便它在全球范围内可用,因此我的Node.js代码可与我的应用程序中任何位置的任何异步函数/生成器babel转译一起使用。 regenerator was installed via npm npm install regenerator . regenerator是通过npm install regenerator

My question is, why does this code 我的问题是,为什么这段代码

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

not work as expected, leading to an undefined being logged, while replacing the first line with 不能按预期工作,导致记录未定义,同时将第一行替换为

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

leads to expected results. 导致预期的结果。

Looking in the runtime file I see this code 在运行时文件中查看此代码

runtime = global.regeneratorRuntime = inModule ? module.exports : {};

in an IIFE with this expression passed in as global 在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
);

which I would expect to properly set up regenratorRuntime on the global object. 我希望可以在全局对象上正确设置regenratorRuntime

I don't mind manually setting global.regenratorRuntime , but I would like to understand why it's necessary. 我不介意手动设置global.regenratorRuntime ,但是我想了解为什么这样做是必要的。 It seems as though code Node executes from a require statement may be acting differently than I assumed. 似乎Node从require语句执行的代码的行为可能与我假设的有所不同。

As an ancillary matter, can anyone point out what the self check is checking for? 作为辅助问题,任何人都可以指出self检查要检查的内容吗?

It does set 它确实

global.regeneratorRuntime
//          ^

not

global.regenratorRuntime

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

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