4 ms·
Using generators, you can also convert any callback code into a serial one. Here's a demo: https://github.com/vjeux/AsyncAwait https://github.com/vjeux/AsyncAw
by vjeux 14y ago
Using generators, you can also convert any callback code into a serial one.
Here's a demo: https://github.com/vjeux/AsyncAwait https://github.com/vjeux/AsyncAwait
Compare
function main() {
multiply(1, 2, function (res) {
multiply(res, 3, function (res) {
multiply(res, 4, function (res) {
console.log(res);
})
})
})
}
main() // 24
to
var main = async(function () {
var [res] = yield await(multiply)(1, 2);
[res] = yield await(multiply)(res, 3);
[res] = yield await(multiply)(res, 4);
console.log(res);
});
main() // 24