javascript - Bluebird promises and catch branching -


i'm wondering if there way in bluebird promises .catch thrown error , process specific actions without branching (nested promise).

say 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 executed if customerror has been thrown   console.log("let's try again");   return dosomethingasync(); })  .catch(customerror2, function (error) {   // oh customerror2! }) .delay(15000) // don't try again ! .then(function () {   // should executed if customerror2 has been thrown   console.log("let's try again after long delay");   return dosomethingasync(); })  .catch(function (error) {   // should catch random errors of chain }) 

when execute code, several behaviours:

  • if no error throws, "success, great!" , start again "let's try again after long delay"
  • if customerror throws, "let's try again"
  • if customerror2 throws, "let's try again after long delay"

i can't figure out what's happening flow. should great write instead of nesting errors' specific code in new promise chains.

.catch thrown error , process specific actions without branching

no. because is branching. nesting totally natural here. think of using synchronous-try-catch metaphor , same.

i can't figure out what's happening flow.

  • if no error thrown, "success, great!" , start again "let's try again after long delay"

hm, that's odd, because "let's try again" (without delay) chained before that. should both logs. chain processed sequentially:

dosomethingasync() // returns result  return result // first callback: continues  console.log('success, great!') // next callback: logs  catch // ignored because no rejection  console.log("let's try again"); // next callback: logs       return dosomethingasync();      //                , returns result  catch // result, not being rejection, ignored here  delay // fulfillment delayed  console.log("let's try again after long delay"); // next callback again logs       return dosomethingasync();                            // , returns result  catch // ignored again 
  • if customerror thrown, "let's try again"

yes, because savesomethingasync();, result of previous promise, fulfilled, next .then() callback in chain executes.

  • if customerror2 thrown, "let's try again after long delay"

yes, because error bubbled way down .catch(customerror2, …) handled. on way, no callbacks executed. after the error handled, promise fulfilled, , next .then() in chain calls callback.

i think want here is

dosomethingasync() .then(function(result) {   if (!result)     // customerror('blah blah');     // oh customerror!     return savesomethingasync();     .then(function(saved) {       console.log("let's try again");       return dosomethingasync();     });   else if (result == 0)     // customerror2('blah blah blah');     // oh customerror2!     return promise.delay(15000) // don't try again !     .then(function() {       console.log("let's try again after long delay");       return dosomethingasync();     })   else     console.log('success, great!');   // return undefined implied }).catch(function (error) {   // catch random errors of chain   // thrown of dosomethingasync() or of savesomethingasync }) 

Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -