Skip to content

Instantly share code, notes, and snippets.

@mightyguava
Last active March 12, 2017 19:19
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 mightyguava/b255c236e1e22a0e90ebee8aff2661b6 to your computer and use it in GitHub Desktop.
Save mightyguava/b255c236e1e22a0e90ebee8aff2661b6 to your computer and use it in GitHub Desktop.
Demystifying Async Programming in Javascript - co() simple challenge
function deferred(val) {
return new Promise((resolve, reject) => resolve(val));
}
co(function* asyncAdds() {
console.log(yield deferred(1)); // 1
console.log(yield deferred(2)); // 2
console.log(yield deferred(3)); // 3
return 4;
}).then(function (result) {
console.log(result); // 4
});
function co(generator) {
return new Promise((resolve, reject) => {
// Your code goes here
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment