Created
July 12, 2013 21:27
-
-
Save domenic/5987999 to your computer and use it in GitHub Desktop.
Iterator of promises: forEach
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
function iteratorOfPromisesForEach(iterator, callback) { | |
var snapshot = iterator.next(); | |
if (snapshot.done) { | |
return Promise.resolve(); | |
} | |
return snapshot.value.then(callback).then(() => iteratorOfPromisesForEach(iterator, callback)); | |
} | |
var result = iteratorOfPromisesForEach(filesIterator, function (file) { | |
// return immediately to just do sync processing | |
// or return a promise to delay pulling new results until the promise fulfills | |
// any rejections, either of the promises in the iterator or caused by `callback` | |
// throwing or caused by `callback` returning a rejected promise, flow to `result` | |
// if there were no rejections `result` is fulfilled with `undefined`. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's also possible to do it this way: