Dan*_*ham 5 javascript node.js async-await es6-promise babeljs
我有以下节点应用程序包含异步函数,等待ES6承诺.
async function test(id){
try {
let val = await Promise.resolve(id);
console.log("val: " + val);
} catch (error) {
console.log("error: " + error);
}
}
test(1);
Run Code Online (Sandbox Code Playgroud)
结果= val:未定义
预期结果:val:1
我使用gulp-babel将其编译为ES5.
我在gulp任务中有以下设置:
.pipe(babel({ optional: ["es7.asyncFunctions"] }))
Run Code Online (Sandbox Code Playgroud)
在安装babel之后我也要求'babel/polyfill'.
透明代码:
function test(id) {
var val;
return regeneratorRuntime.async(function test$(context$1$0) {
while (1) switch (context$1$0.prev = context$1$0.next) {
case 0:
context$1$0.prev = 0;
context$1$0.next = 3;
return Promise.resolve(id);
case 3:
val = context$1$0.sent;
console.log('val: ' + val);
context$1$0.next = 10;
break;
case 7:
context$1$0.prev = 7;
context$1$0.t0 = context$1$0['catch'](0);
console.log('error: ' + context$1$0.t0);
case 10:
case 'end':
return context$1$0.stop();
}
}, null, this, [[0, 7]]);
}
test(1);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1457 次 |
| 最近记录: |