Bluebird - how to spread errors in nested promises

PromiseA().then(function(){
    PromiseB().catch(function (e) {
        throw e;
    })
}).catch(function (e) {
    // I want exceptions thrown in the nested promise chain to end up here.
})

How to get exceptions from nested promises for bubbles before parent promise?

+4
source share
2 answers

Use the keyword return,

your code can be simplified as:

PromiseA().then(function(){
    return PromiseB();
}).catch(function (e) {
    // I want exceptions thrown in the nested promise chain to end up here.
})

Edit:

Not sure if this is the right way, but if you want to void a promise, and you only want to create a stream of errors, you can wrap your promise in a custom promise that never resolves, but throws an error (if that happens):

PromiseA().then(function(){
    var myPromise = PromiseB().then...;
    return new Promise(function(resolve, reject){
      myPromise.catch(reject)
    })
}).catch(function (e) {
    // I want exceptions thrown in the nested promise chain to end up here.
})
+3
source

, promises. return . PromiseB , PromiseB.

Promise.reject, :

PromiseA()
    .then(function() {
        return PromiseB()
            .catch(function (err) {
                // TODO: error processing
                return Promise.reject(err);
            });
    })
    .catch(function(err) {
        // Here you'll get the same err object as in first catch
    });
+2

All Articles