Skip to content

Instantly share code, notes, and snippets.

@stormoz
Created July 28, 2016 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stormoz/e47d25a716e689b9f51282113895fc04 to your computer and use it in GitHub Desktop.
Save stormoz/e47d25a716e689b9f51282113895fc04 to your computer and use it in GitHub Desktop.
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