javascript - chaining recursive promise with bluebird -


i have promise chain recursive promise doasyncrecursive() in middle so:

doasync().then(function() {     return doasyncrecursive(); }).then(function() {     return dosomethingelseasync(); }).then(function(result) {     console.log(result); }).catch(errorhandler); 

doasyncrecursive() has , if @ first not succeed, after want try every 5 seconds until does. promise function looks like:

function doasyncrecursive() {     return new promise(function(resolve, reject) {         //do async thing         if (success) {             resolve();         } else {             return new promise(function(resolve, reject) {                 settimeout(function() {                     doasyncrecursive();                 }, 5000);             });         }     }); } 

but when execute, chain not continue after doasyncrecursive() successful on 2nd try , resolve() called (it continues if attempt successful on 1st try however).

what pattern need make work?

catch failure, wait 5 seconds, try again.

function doasyncrecursive() {     return doasyncthing().catch(function() {         return promise.delay(5000).then(doasyncrecursive);     }); } 

here doasyncthing function corresponding //do async thing comment in op's code, defined returning promise. in original code, success or failure of "do async thing" tested using success flag, definition asynchronous routines not deliver such flag; deliver results either via callback or promise. code above assumes doasyncthing returns promise. assumes "failure", in sense of "does not return response want", represented promise rejecting. if instead "success" or "failure" defined particular value of fulfilled promise, you'd want do

function doasyncrecursive() {     return doasyncthing().then(function(success) {         if (success) return success;         else return promise.delay(5000).then(doasyncrecursive);     }); } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -