异步生成器函数

时间:2019-06-14 09:36:29

标签: javascript node.js meteor promise babeljs

我正在尝试在流星应用程序中实现异步生成器功能。但是由于以下错误,我无法使它工作:

W20190614-11:14:39.757(2)? (STDERR) (node:16217) UnhandledPromiseRejectionWarning: TypeError: Object is not async iterable
W20190614-11:14:39.757(2)? (STDERR)     at _asyncIterator (/Users/sst/entw/resy/resyStat/node_modules/@babel/runtime/helpers/asyncIterator.js:16:9)
W20190614-11:14:39.757(2)? (STDERR)     at Promise.asyncApply (server/main.js:33:3)
W20190614-11:14:39.757(2)? (STDERR)     at /Users/sst/.meteor/packages/promise/.0.11.2.pg5aj7.wk7c++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/meteor-promise/fiber_pool.js:43:40
W20190614-11:14:39.757(2)? (STDERR) (node:16217) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
W20190614-11:14:39.757(2)? (STDERR) (node:16217) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这是我编写的用于重现该错误的代码:

// server/main.js

async function* counter(max) {
  let count = 0;
  while (count <= max) {
    yield await Promise.resolve(count++);
  }
}

(async () => {
  for await (const num of counter(3)) {
    console.log(num);
  }
})();

我的项目设置:

// Meteor: v1.8.1

// package.json
{
  "dependencies": {
    "@babel/runtime": "^7.4.5",
    ...
  },
  "devDependencies": {
    "@babel/plugin-transform-runtime": "^7.4.4",
    "babel-plugin-transform-decorators-legacy": "^1.3.5",
    ...
  },
  ...
}

// .babelrc
{
  "presets": ["env"],
  "plugins": ["decorators-legacy", "@babel/plugin-transform-runtime"]
}

这是我已经调试的:

const it = counter(3);
console.log(it); // { _invoke: [Function send] }

console.log(it[Symbol.iterator]);      // undefined
console.log(it[Symbol.asyncIterator]); // undefined

我以前没有安装@babel/plugin-transform-runtime,但是建议将它与their docs上的@babel/runtime一起使用,所以我安装了它。对您没有太大帮助:/

编辑:
也许我应该澄清一下:我认为此错误与babel有点相关,因为当我尝试获取函数it[Symbol.asyncIterator]时-它不存在。通常,此函数会生成迭代器对象,但它不存在。

0 个答案:

没有答案
相关问题