I have a promise chain with a recursive promise doAsyncRecursive() in the middle like so:
doAsync().then(function() {
return doAsyncRecursive();
}).then(function() {
return doSomethingElseAsync();
}).then(function(result) {
console.log(result);
}).catch(errorHandler);
doAsyncRecursive()
has to do something and if it at first does not succeed, i then after want to try every 5 seconds until it does. This is what my 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 I execute, the chain does not continue after doAsyncRecursive()
is successful on the 2nd try and resolve()
is called (it continues if the attempt is successful on the 1st try however).
What pattern do I need to make this work?