I'm wondering if there a way in Bluebird promises to .catch
a thrown error and then process some specific actions without branching (nested promise).
Say I have
doSomethingAsync()
.then(function (result) {
if (!result)
throw new CustomError('Blah Blah');
if (result == 0)
throw new CustomError2('Blah Blah Blah');
return result;
})
.then(function (result) {
console.log('Success, great!');
})
.catch(CustomError, function (error) {
// Oh CustomError!
return saveSomethingAsync();
})
.then(function (saved) {
// Should only be executed if an CustomError has been thrown
console.log("Let's try again");
return doSomethingAsync();
})
.catch(CustomError2, function (error) {
// Oh CustomError2!
})
.delay(15000) // Don't try again now !
.then(function () {
// Should only be executed if an CustomError2 has been thrown
console.log("Let's try again after some long delay");
return doSomethingAsync();
})
.catch(function (error) {
// Should catch all random errors of the chain
})
When I execute this code, I get several behaviours:
- If no error throws, I get "Success, great!" and it start again with "Let's try again after some long delay"
- If CustomError throws, I get "Let's try again"
- If CustomError2 throws, I get "Let's try again after some long delay"
I can't figure out what's happening with this flow. Should be great to write something like this instead of nesting the errors' specific code in new promise chains.