Created
July 28, 2016 21:51
-
-
Save stormoz/e47d25a716e689b9f51282113895fc04 to your computer and use it in GitHub Desktop.
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
function callAsync(fn) { | |
// get iterator | |
var gen = fn(); | |
// used as a callback to currently yielded functions | |
function next() { | |
// get next function | |
var ret = gen.next(); | |
// recursion exit condition | |
if (ret.done) { return; } | |
// call async function and pass next() as aa callback so it will ask the iterator to yield next when callback is called | |
ret.value(next); | |
} | |
// initialise recursion | |
next(); | |
} | |
function foo(timeout) { | |
return function (done) { | |
setTimeout(() => { | |
console.log(`done ${timeout}`); | |
done(); | |
}, timeout); | |
} | |
} | |
callAsync(function* () { | |
var a = yield foo(1000); | |
var b = yield foo(2000); | |
console.log('done all'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment