1

I'm still fairly new to promises, and all the roads on what I'm currently doing lead me to Promise.all(), but I'm not sure how to handle the results.

I have some data like this

let data = {
  "format_oid":["35474527960032","2595837960032"]
};

I then pass it to a function like

server.js

jobHandler.handle(data)
  .then((info) => {
    console.log(info);
  })
  .catch((err) => {
    console.error(err);
  });

Within my jobHandler class I have

jobHandler.js

class Handler {
  // constructor etc
  handle(data) {
    return this.extract(data);
  }

  extract(data) {
    let request = data.format_oid.map( (releaseId) => {
      return this._getReleaseId(releaseId);
     });
     return Promise.all(request);
  }

  _getReleaseId(releaseId) {
    return new Promise( (resolve, reject) => {
      if (_hasRelease(releaseId)){
        resolve('yay');
      } else {
        reject('boo');
      }
    });
}

Currently request will have an array of Promises like [ Promise { <pending> }, Promise { <pending> } ]

how would I then handle this array of promises in my server.js?

Jarede
  • 3,310
  • 4
  • 44
  • 68

1 Answers1

4

One promise resolves with one result. Promise.all() resolves to an array of results, one result for each promise that was in the original array of promises in the same order as the promises.

how would I then handle this array of promises in my server.js?

aHandlerObject.extract(data).then(results => {
   // results is an array of resolved values
   // one for each call to this._getReleaseId(...)
}).catch(err => {
   // handle error here
});
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • 1
    Am I missing something? This seems to be the same code I have already? Hmm I think I've asked the wrong question. If one of my data returns 'yay' and the other 'boo', yay never seems to be output with my current code. I think i need a new question for this. – Jarede Jan 19 '18 at 10:22
  • 1
    Further reading seems to suggest that Promise.all will flunk out as soon as the array of promises has a rejection and not display any of the resolved values for that array... – Jarede Jan 19 '18 at 11:13
  • @Jarede - You asked a question (which I show in yellow in my answer) and I answered that for you. It seemed to me that you were asking how the result from `Promise.all()` worked and how you would use it so that's what I answered. – jfriend00 Jan 19 '18 at 16:56
  • @Jarede - As for the specific behavior of `Promise.all()`, it will reject immediately if any promise you pass it rejects. If you want a different behavior, you can either intercept any of your promises with a `.catch()` on them individually to turn a reject into a resolve or you can use some code that does that for you: [ES6 Promise.all() error handle - Is .settle() needed?](https://stackoverflow.com/questions/36605253/es6-promise-all-error-handle-is-settle-needed/36605453#36605453) – jfriend00 Jan 19 '18 at 16:57
  • Yep @jfriend00 I asked the wrong question initially. My bad. I'm still fairly new to promises so still a bit to figure out. That link helps, though i ended up using an answer from the duplicate question flag. Cheers though. – Jarede Jan 19 '18 at 17:03