javascriptarraysforeachpromisees6-promise

How to use promise in forEach loop of array to populate an object


I am running a forEach loop on an array and making two calls which return promises, and I want to populate an object say this.options, and then do other stuff with it. Right now I am running into the async issue if i use the following code sample and i get into the then function first.

$.when.apply($, someArray.map(function(item) {
    return $.ajax({...}).then(function(data){...});
})).then(function() {
    // all ajax calls done now
});

This is working code below, but it only works for the first element in the array, because I call the resulting function in the .then of the response. I want to do all the fetch first for all elements of the array and then call the resulting function to do something.

array.forEach(function(element) {
    return developer.getResources(element)
        .then((data) = > {
            name = data.items[0];
            return developer.getResourceContent(element, file);
        })
        .then((response) = > {
            fileContent = atob(response.content);
            self.files.push({
                fileName: fileName,
                fileType: fileType,
                content: fileContent
            });
            self.resultingFunction(self.files)
        }).catch ((error) = > {
            console.log('Error: ', error);
        })
});

How do i populate the self.files object after the forEach loop is complete, and then call the resulting function with the files object?


Solution

  • Promise.all() will be helpful here:

    var promises = [];
    
    array.forEach(function(element) {
        promises.push(
            developer.getResources(element)
                .then((data) = > {
                    name = data.items[0];
                    return developer.getResourceContent(element, file);
                })
                .then((response) = > {
                    fileContent = atob(response.content);
                    self.files.push({
                        fileName: fileName,
                        fileType: fileType,
                        content: fileContent
                    });
                }).catch ((error) = > {
                    console.log('Error: ', error);
                })
        );
    });
    
    Promise.all(promises).then(() => 
        self.resultingFunction(self.files)
    );
    

    This starts the AJAX call for each of the items, adds the result of each call to self.files once the call is complete and calls self.resultingFunction() after all calls have been completed.

    Edit: Simplified based on Yury Tarabanko's suggestions.