1

When I try to use Promise.all to call the same internal endpoint multiple times the data returned from each promise in a order different every time. For additional information: These endpoints perform significant DB queries to retrieve decently large arrays of data (10X20 array of strings)

Why is this happening?

Isn't Promise.all suppose to run sequentially. Is this issue due to the fact that I'm calling the same endpoint every time?

I have not encountered this issue before when using Promise.all to call multiple different endpoints

// endpoint return "1"
getCall1(){
   return new Promise((resolve, reject) => { internal_api.get(`get/users/`, data = {'name': 1})
        .then(res => {resolve(res);})
        .catch(err => {reject(err);})
   });
}
// endpoint return "2"
getCall2(){
   return new Promise((resolve, reject) => { internal_api.get(`get/users/`, data = {'name': 2})
        .then(res => {resolve(res);})
        .catch(err => {reject(err);})
   });
}
// endpoint return "3"
getCall3(){
   return new Promise((resolve, reject) => { internal_api.get(`get/users/`, data = {'name': 3})
        .then(res => {resolve(res);})
        .catch(err => {reject(err);})
   });
}

getAllCalls(){

    Promise.all([
        this.getCall1(),
        this.getCall2(),
        this.getCall3(),
      ]).
   .then(([call1, call2, call3]) => {
      console.log(call1, call2, call3)
   })
   .catch();

Run #1 console logs


2 1 3

Run #2 console logs


3 1 2

Run #3 console logs


1 1 2

Run #4 console logs


1 2 3

Why is this happening?

Isn't Promise.all suppose to run sequentially. Is this issue due to the fact that I'm calling the same endpoint every time?

I have not encountered this issue before when using Promise.all to call multiple different endpoints

  • 2
    `Isn't Promise.all suppose to run sequentially.` Nope, it's not. And that's the whole point, because with promises you know when they are called but not when they are fulfilled. From the documentation: ```The Promise.all() method takes an iterable of promises as input and returns a single Promise. This returned promise fulfills when all of the input's promises fulfill (including when an empty iterable is passed), with an array of the fulfillment values.``` I suggest you to read the doc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all – Arnau Nov 17 '22 at 08:36
  • 1
    note: since `internal_api.get` already returns a Promise, no need to wrap those calls in a promise constructor as that is considered an anti-pattern – Jaromanda X Nov 17 '22 at 08:38
  • Further to @Arnau - the iterable (array) doesn't have to be ALL (or even ANY) promises - but that's probably going to cause confusion - you're welcome – Jaromanda X Nov 17 '22 at 08:39
  • I see, but can you help shed some light on why the problem I am having is occuring? – 1daioo123mmd Nov 17 '22 at 08:42
  • because you're executing your `getCallN` in parallel - and there's no guarantee in what order those api calls will resolve – Jaromanda X Nov 17 '22 at 08:47
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Nov 17 '22 at 08:51
  • If you expect the `internal_api.get()` call to return the value you pass in the `name` object, then it should always return `1, 2, 3`. If not, then it could be an issue on your server's side. `Promise.all()` (if awaited) should result in an array with the result of each Promise you passed to it, in the same order you passed them to `Promise.all()`. Example: https://jsfiddle.net/au67x2j8/ – Ivar Nov 17 '22 at 08:57

0 Answers0