Search code examples
javascriptnode.jsexceptionpromisebluebird

How can you retry after an exception in Javascript when using promises?


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);
        });
}

Solution

  • 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); 
      });
    }