0

I have a promises array:

let promises = [];
let teamsLooped = 0;
topTeams.teams.forEach(function(value){promises.push({id: value.teamId})});

I pass that promise array to an API function like this and I need to recover the teamId during resolution to assign the output to the right team when I'm saving the file:

Promise.all(promises.map(o => API.someFunction(o))).then((res) => {
let teamId = promises[teamsLooped].id;
teamsLooped++;
// Rest of the code
...
fs.writeFile("team "+ teamId +".json", JSON.stringify(statsExport), function(err) {
           console.log("Graba team: ", teamId)
            if (err) {
                console.log(err);
            }
        })
}

So right now I am assuming that .then() method will be processing the API outputs in the order of the promises array.

My question is: is it reasonable to assume that since all promises have resolved, that the code will loop through them in the same order they were passed (i.e. order of the promises array), or is the .then() code processed in some other order?

  • 1
    On the client side, the order in general will be preserved. See: https://stackoverflow.com/questions/28066429/promise-all-order-of-resolved-values for a good explanation. – dwosk Oct 02 '20 at 20:27

1 Answers1

1

It is a bit confusing exactly what you're asking based on the code you show. I will attempt to explain what I think you might be asking.

Let's say you have a set of code that creates 10 promises that each resolve in a random time and they keep track of when they were launched and when they finished. You can then see the results and see that the array is in the order of the original array, even though they finished in a much different order:

let finishCntr = 0;

function randDelay(val) {
    let t = Math.floor(Math.random() * 3000);
    return new Promise(resolve => {
        setTimeout(() => {
            let finishSequence = finishCntr++;
            resolve({ launchSequence: val, finishSequence, t });
        }, t);
    });
}

let values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

// create array of promises from the values array
let promises = values.map(launchSequence=> {
    return randDelay(launchSequence);
});

// wait for all the promises to finish
Promise.all(promises).then(results => {
    // log the final results
    console.log(results);
}).catch(e => {
    console.log(e);
});

When you run this, you will see that the array of results is in the order of launchSequence which is the order of the original array of values.

So right now I am assuming that .then() method will be processing the API outputs in the order of the promises array.

Promise.all() collects all the resolved results from an array of promises, keeps those results in the order they were in the original array of promises and then calls the .then() or .catch() handlers when all the promises are done or when a promise rejects.

My question is: is it reasonable to assume that since all promises have resolved, that the code will loop through them in the same order they were passed (i.e. order of the promises array), or is the .then() code processed in some other order?

Your code does not loop through the promise results at all so it's a bit confusing exactly what you're asking in relation to the code you show. The resolved array of results would be in the res argument to the .then() handler. If you do have code that iterates through that array, then those results will be in the order of the original promises, regardless of what order they finished in.


Other Comments:

Your array named promises is very misleading. It is not an array of promises. It's an array of objects, you create here:

topTeams.teams.forEach(function(value){promises.push({id: value.teamId})});

which, by the way could be done as this:

const arrayOfObjects = topTeams.teams.map(value => return {id: value.teamId});
jfriend00
  • 683,504
  • 96
  • 985
  • 979