Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save 7flash/112a8ff19c7148a729383fef3ef2cd4e to your computer and use it in GitHub Desktop.
Save 7flash/112a8ff19c7148a729383fef3ef2cd4e to your computer and use it in GitHub Desktop.
var co = require('co');
let items = [1,2,3];
function* getResult(item) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(item * 2), 100);
});
}
co(function* () {
for(let item of items) {
let itemResult = yield getResult(item);
console.log(itemResult);
}
});
@7flash
Copy link
Author

7flash commented May 17, 2017

Note: in this case you should not use infinite sequence generator instead of items array because for-of loop is synchronous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment