1// The array to be iterated over can be a mix of values and promises.
2var fileNames = ["1.txt", Promise.resolve("2.txt"), "3.txt", Promise.delay(3000, "4.txt"), "5.txt"];
3
4Promise.each(fileNames, function(fileName, index, arrayLength) {
5 // The iteration will be performed sequentially, awaiting for any
6 // promises in the process.
7 return fs.readFileAsync(fileName).then(function(fileContents) {
8 // ...
9
10 // The final resolution value of the iterator is is irrelevant,
11 // since the result of the `Promise.each` has nothing to do with
12 // the outputs of the iterator.
13 return "anything"; // Doesn't matter
14 });
15}).then(function(result) {
16 // This will run after the last step is done
17 console.log("Done!")
18 console.log(result); // ["1.txt", "2.txt", "3.txt", "4.txt", "5.txt"]
19});
20