我们正在尝试使用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
。