Iterate an array with a promise in javascript

Example of how to iterate an array with a promise

It can be challenging to figure out how to iterate over an array of items with a promise.

The following code takes a callback function that may or may not be a promise already.

Once inside the PromiseForEach function, the callback will become a promise (if it isn’t already).

You also have the option of pushing the values onto an output array,  responseArray in the example below.

The response from the callback may be pushed directly onto the responseArray resulting in an array of promises that may be further manipulated, although at this point I am not sure why one would do that.

 

Leave a Reply

Your email address will not be published. Required fields are marked *