0

I am still quite new to this and perhaps promise.all() is not what I need, or maybe I am just doing this the wrong way to begin with, but I have a bunch of resources I am trying to fetch via HTTP.

Some of them will not be available to certain account types, so I figure, let the ones that fail due to server-side permission fail and just get on with it.

promise.all([
    http.get('bookings'),
    http.get('users'),
    http.get('listings')
])
.then(valuse => assign(values))
.catch(err => makeAnError(err))

So assuming http.get('users') fails, the promise.all will reject and none of the other values will be assigned as the resolve function to assign those values never runs.

What are some suggested solutions to this?

David Alsh
  • 6,747
  • 6
  • 34
  • 60
  • simple answer, handle individual rejections – Jaromanda X Mar 11 '17 at 01:57
  • The `async` package for Node has some good control flow capabilities that may be of use, in particular the `async.series` function. – m_callens Mar 11 '17 at 01:59
  • @m_callens - `asyncjs + Promises == bad design` (I'm not referring to async/await) - besides, `async.series` performs the actions in series, whereas the code in the question performs them in parallel (as much as that's possible in js) – Jaromanda X Mar 11 '17 at 02:00

1 Answers1

0

I do it this way

Promise.prototype.always = function always() {
    return this.then(result=>({fulfilled: true, result}), reason=>({fulfilled: false, reason}));
};

then your code

promise.all([
    http.get('bookings').always(),
    http.get('users').always(),
    http.get('listings').always()
])
.then(values => assign(values)) // assign needs to check each value.fulfilled == true before using value.result
.catch(err => makeAnError(err))
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87