Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
98
rated 0 times [  99] [ 1]  / answers: 1 / hits: 22630  / 9 Years ago, wed, may 27, 2015, 12:00:00

I'm using the Bluebird promise library. I have a chain of promisified functions like the following:



    receiveMessageAsync(params)
.then(function(data)) {
return [data, handleMessageAsync(request)];
})
.spread(function(data, response) {
return [response, deleteMessageAsync(request)];
})
.spread(function(response, data) {
return sendResponseAsync(response);
})
.then(function(data) {
return waitForMessage(data);
})
.catch (function(err) {
// handle error here
});


Occasionally sendMessage will fail because, let's say, the server to respond to isn't available. I want the code to keep on trying to respond forever until it succeeds. You can't simply wrap the sendMessage in a catch because it doesn't actually throw an exception, I suppose, it calls the error function which, in this promisified code is the catch at the bottom. So there must be some way to retry send message in the catch section. The problem is that even if I retry in a loop in the catch I still have no way to jump up to the promise chain and execute the remaining promisified functions. How do I deal with this?



EDIT:



My retry for a HTTP post ended up looking like this:



function retry(func) {
return func()
.spread(function(httpResponse) {
if (httpResponse.statusCode != 200) {
Log.error(HTTP post returned error status: +httpResponse.statusCode);
Sleep.sleep(5);
return retry(func);
}
})
.catch(function(err) {
Log.err(Unable to send response via HTTP);
Sleep.sleep(5);
return retry(func);
});
}

More From » node.js

 Answers
3

Here's a sample retry function (not yet tested):



function retry(maxRetries, fn) {
return fn().catch(function(err) {
if (maxRetries <= 0) {
throw err;
}
return retry(maxRetries - 1, fn);
});
}


The idea is that you can wrap a function that returns a promise with something that will catch and retry on error until running out of retries. So if you're going to retry sendResponseAsync:



receiveMessageAsync(params)
.then(function(data)) {
return [data, handleMessageAsync(request)];
})
.spread(function(data, response) {
return [response, deleteMessageAsync(request)];
})
.spread(function(response, data) {
return retry(3, function () { return sendResponseAsync(response); });
})
.then(function(data) {
return waitForMessage(data);
})
.catch (function(err) {
// handle error here
});


Since the retry promise won't actually throw until all retries have been exhausted, your call chain can continue.



Edit:



Of course, you could always loop forever if you preferred:



function retryForever(fn) {
return fn().catch(function(err) {
return retryForever(fn);
});
}

[#66458] Sunday, May 24, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
michaelashelbieh

Total Points: 303
Total Questions: 139
Total Answers: 97

Location: Suriname
Member since Sun, Oct 17, 2021
3 Years ago
;