Created
October 7, 2018 05:05
-
-
Save hg-pyun/504ee2546ff5a8931fefd85571c1e894 to your computer and use it in GitHub Desktop.
async-wait.01.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ES7 | |
async function foo() { | |
await bar(); | |
} | |
// ES5 complied | |
let foo = (() => { | |
var _ref = _asyncToGenerator(function*() { | |
yield bar(); | |
}); | |
return function foo() { | |
return _ref.apply(this, arguments); | |
}; | |
})(); | |
function _asyncToGenerator(fn) { | |
return function() { | |
var gen = fn.apply(this, arguments); | |
return new Promise(function(resolve, reject) { | |
function step(key, arg) { | |
try { | |
var info = gen[key](arg); | |
var value = info.value; | |
} catch (error) { | |
reject(error); | |
return; | |
} | |
if (info.done) { | |
resolve(value); | |
} else { | |
return Promise.resolve(value).then( | |
function(value) { | |
step("next", value); | |
}, | |
function(err) { | |
step("throw", err); | |
} | |
); | |
} | |
} | |
return step("next"); | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment