I have a function that results in a lot of errors so I have to call it many times before it finally gives the right result. It returns a promise so I created a wrapper around it that recursively keeps calling it upon rejection.
I want to return a new Promise created via Bluebird but which has to reject after a set timeout. And it has to keep calling that above function repeatedly. But before each repetition I want to check whether it was auto-rejected due to timeout.
Bluebird has a isRejected()
method but it seems I can't use it from within the promise body:
var promise = new Promise(function(resolve, reject){
var self = this;
setTimeout(reject, TIMEOUT*1000);
return doSomethingWrapper();
function doSomethingWrapper(){
if(promise.isRejected()) return;
// Error: Cannot read property 'isRejected' of undefined
if(self.isRejected()) return;
// Error: self.isRejected is not a function
return doSomething().then(resolve, doSomethingWrapper).catch(doSomethingWrapper);
}
});
Any other solution?