Node.js Babel变换异步到模块方法到带ES6映射的Bluebird
我们正在尝试将Node.js 6.5.0与Node.js Babel变换异步到模块方法到带ES6映射的Bluebird,node.js,ecmascript-6,babeljs,bluebird,Node.js,Ecmascript 6,Babeljs,Bluebird,我们正在尝试将Node.js 6.5.0与Babel结合使用,以实现异步函数使用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": "^
Babel
结合使用,以实现异步函数
使用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"
}
]
]
}
但是,我们返回ES6 map的异步函数在执行过程中会导致以下错误:
生成的值[对象映射]不能视为承诺
我们如何解决这个问题
当异步函数
通过将异步函数转换为生成器
,将异步函数转换为生成器
,以下是一些触发相同错误的示例代码:
function giveMap() {
return new Map();
}
void async function() {
await giveMap();
}();
请注意,giveMap
没有标记为async
(这是实际问题)
使用transform async to generator
时,此代码将运行,因为Map
可从生成器中接受:
function* () {
yield new Map();
}
但是,当使用将异步转换为模块方法时,我认为代码与此类似:
Promise.coroutine(function* () {
yield new Map();
});
这将导致错误,因为Promise.coroutine()
希望产生承诺
因此,您应该注意那些返回映射
、启用了等待
、但未映射为异步的函数,谢谢。这很有道理。我们会调查并让你知道。