Unable to resolve promise when using async wait with babel and ES6 promise

I have the following node application containing an async function awaiting ES6 promises.

async function test(id){ try { let val = await Promise.resolve(id); console.log("val: " + val); } catch (error) { console.log("error: " + error); } } test(1); 

Result = val: undefined

Expected Result: val: 1

I am using gulp -babel to compile this in ES5.

I have the following gulp task set:

 .pipe(babel({ optional: ["es7.asyncFunctions"] })) 

I also need "babel / polyfill" after npm installing babel.

Recoded Code:

 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); 
+5
source share
1 answer

You seem to be using Babel version up to Babel version 5.5.0 . Before this version, you could install regenerator (Babel dependency) on version less than 0.8.28 , which is the first version where regenerator started supporting await Promise.resolve(value) (the code in your example).

Support was added to this commit on the regenerator side, and Babel was updated to require at least 0.8.28 release of the regenerator with this commit.

0
source

All Articles