将Babel转换为asynchronous模块的方法转换为带有ES6映射的Bluebird
我们正在尝试在Babel
使用Node.js 6.5.0,使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", }
和.babelrc
:
{ "plugins": [ "transform-es2015-modules-commonjs", "transform-es2015-destructuring", [ "transform-async-to-module-method", { "module": "bluebird", "method": "coroutine" } ] ] }
但是,我们的async functions
返回ES6映射在执行过程中导致以下错误:
一个价值[对象地图]被产生,不能被视为一个承诺
我们如何解决这个问题?
聚苯乙烯一切正常,当async functions
转换为generators
与transform-async-to-generator
以下是一些触发相同错误的示例代码:
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
。