Last active
July 4, 2016 16:43
-
-
Save rehia/2f0438a14a05b2cf2b479ddf58fe61f2 to your computer and use it in GitHub Desktop.
yielding array of promises
This file contains hidden or 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
'use strict'; | |
const fs = require('fs'); | |
const co = require('co'); | |
const files = ['./plan.md', './karma.conf.js', './package.json', './LICENSE']; | |
function checkFile(file) { | |
return new Promise((fulfill, reject) => { | |
console.log('checking file', file); | |
fs.stat(file, (error) => { | |
console.log('file checked', file); | |
if (error) { | |
return reject(error); | |
} | |
fulfill(); | |
}); | |
}); | |
} | |
Array.prototype.mapIterable = function* mapIterable(map) { | |
for (var i = 0; i < this.length; i++) { | |
yield map(this[i]); | |
} | |
}; | |
co(function* main() { | |
console.log('1 - with Promise.all'); | |
yield Promise.all(files.map(checkFile)); | |
console.log(''); | |
console.log('2 - yielding checks with yield*'); | |
yield* files.map(checkFile); | |
console.log(''); | |
console.log('3 - yielding each check in for...of'); | |
for (const file of files) { | |
yield checkFile(file); | |
} | |
console.log(''); | |
console.log('4 - yielding each checks with mapIterable'); | |
yield* files.mapIterable(checkFile); | |
console.log(''); | |
console.log('the end'); | |
}); |
This file contains hidden or 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
1 - with Promise.all | |
checking file ./plan.md | |
checking file ./karma.conf.js | |
checking file ./package.json | |
checking file ./LICENSE | |
file checked ./plan.md | |
file checked ./package.json | |
file checked ./karma.conf.js | |
file checked ./LICENSE | |
2 - yielding checks with yield* | |
checking file ./plan.md | |
checking file ./karma.conf.js | |
checking file ./package.json | |
checking file ./LICENSE | |
file checked ./plan.md | |
file checked ./karma.conf.js | |
file checked ./package.json | |
file checked ./LICENSE | |
3 - yielding each check in for...of | |
checking file ./plan.md | |
file checked ./plan.md | |
checking file ./karma.conf.js | |
file checked ./karma.conf.js | |
checking file ./package.json | |
file checked ./package.json | |
checking file ./LICENSE | |
file checked ./LICENSE | |
4 - yielding each checks with mapIterable | |
checking file ./plan.md | |
file checked ./plan.md | |
checking file ./karma.conf.js | |
file checked ./karma.conf.js | |
checking file ./package.json | |
file checked ./package.json | |
checking file ./LICENSE | |
file checked ./LICENSE | |
the end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment