«TypeError: объект не асинхронный итеративный» с функцией асинхронного генератора - PullRequest
1 голос
/ 14 июня 2019

Я пытаюсь реализовать функцию асинхронного генератора в моем метеорном приложении.Но я не могу заставить его работать из-за следующей ошибки:

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 установки, но было предложено использовать его с @babel/runtime на их документах так что я его установил.Не очень помогло, хотя: /

РЕДАКТИРОВАТЬ:
Может быть, я должен уточнить: я думаю, что эта ошибка в некоторой степени babel, потому что, когда я пытаюсь получить функцию it[Symbol.asyncIterator] - его не существуетОбычно эта функция создает объект итератора, но его там нет.

...