← Back To API Reference

Promise.each

Promise.each(
    Iterable<any>|Promise<Iterable<any>> input,
    function(any item, int index, int length) iterator
) -> Promise

This article is partially or completely unfinished. You are welcome to create pull requests to help completing this article.

Iterate over an array, or a promise of an array, which contains promises (or a mix of promises and values) with the given iterator function with the signature (value, index, length) where value is the resolved value of a respective promise in the input array. Iteration happens serially. If the iterator function returns a promise or a thenable, then the result of the promise is awaited before continuing with next iteration. If any promise in the input array is rejected, then the returned promise is rejected as well.

If all of the iterations resolve successfully, Promise.each resolves to the original array unmodified. However, if one iteration rejects or errors, Promise.each ceases execution immediately and does not process any further iterations. The error or rejected value is returned in this case instead of the original array.

This method is meant to be used for side effects.

var fileNames = ["1.txt", "2.txt", "3.txt"];

Promise.each(fileNames, function(fileName) {
    return fs.readFileAsync(fileName).then(function(val){
            // do stuff with 'val' here.  
        });
}).then(function() {
    console.log("done");
});

A simple usage example ```js const Promise = require('bluebird'); const path = require('path'); const fs = Promise.promisifyAll(require('fs')); let fileNames = ['a.txt','b.txt','c.txt','d.txt']

// All promises will be executed serrialy the next // iteration will start only after the previous promise is fulfilled return Promise.each(fileNames, file => { return fs.readFileAsync(path.resolve(__dirname, file)).then(data => { console.log(data.toString()); }) // If you use then or catch here it'll be called after each iteration

}).then(arr => { // Will be called after all promises are resolved // arr will just include the original array // so resolt hanle should be inside each iteration }).catch(err => { // Get here if one of the promises was rejected and stop all others

}) ```