Babel转换-async-to-module方法到Bluebird的ES6映射



我们正在尝试使用Node.js 6.5.0与Babel,使async functions使用Bluebird而不是原生V8 ES6承诺:

我们的package.json只包含以下Babel条目:

"devDependencies": {
  "babel-cli": "^6.9.0",
  "babel-plugin-transform-async-to-module-method": "^6.8.0",
  "babel-plugin-transform-es2015-destructuring": "^6.9.0",
  "babel-plugin-transform-es2015-modules-commonjs": "^6.14.0",
}

and .babelrc:

{
  "plugins": [
    "transform-es2015-modules-commonjs",
    "transform-es2015-destructuring",
    [
      "transform-async-to-module-method",
      {
        "module": "bluebird",
        "method": "coroutine"
      }
    ]
  ]
}

但是我们的async functions返回ES6映射在执行过程中导致以下错误:

产生了一个不能被视为承诺的值[object Map]

我们如何解决这个问题?

注:当使用transform-async-to-generator

async functions转换为generators时,一切正常。

下面是触发相同错误的示例代码:

function giveMap() {
  return new Map();
}
void async function() {
  await giveMap();
}();

请注意,giveMap没有标记为async(这是实际问题)。

此代码将在使用transform-async-to-generator时运行,因为Map可以从生成器生成:

function* () {
  yield new Map();
}

然而,当使用transform-async-to-module-method时,我认为代码变得类似于:

Promise.coroutine(function* () {
  yield new Map();
});

这将导致错误,正如这里所解释的,因为Promise.coroutine()期望得到承诺。

因此,您应该注意返回Map的函数,是await ',但没有映射async

最新更新