admin管理员组

文章数量:1202790

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

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);
        });
}
Share Improve this question edited May 27, 2015 at 22:49 Mike asked May 27, 2015 at 0:17 MikeMike 1,7782 gold badges19 silver badges33 bronze badges 3
  • 1 I see no sendMessage here. – Jacob Commented May 27, 2015 at 0:29
  • Sorry, sendResponse should be sendMessage – Mike Commented May 27, 2015 at 15:11
  • I think its interesting that I said I want to retry forever but every answer so far has been written for only a finite number of retries. There must be something about looping forever that bothers people. – Mike Commented May 27, 2015 at 15:12
Add a comment  | 

3 Answers 3

Reset to default 14

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

Here is a small helper that acts like then but retries the function.

Promise.prototype.retry = function retry(onFulfilled, onRejected, n){
    n = n || 3; // default to 3 retries
    return this.then(function(result) {
         return Promise.try(function(){ 
             return onFulfilled(result); // guard against synchronous errors too
         }).catch(function(err){
             if(n <= 0) throw err;
             return this.retry(onFulfilled, onRejected, n - 1);
         }.bind(this)); // keep `this` value
    }.bind(this), onRejected);
};

Which would let you write your code prettier like:

receiveMessageAsync(params)
.then(function(data)) {
    return [data, handleMessageAsync(request)];
})
.spread(function(data, response) {
    return [response, deleteMessageAsync(request)];
})
.retry(function(response, data) {
    return sendResponseAsync(response); // will retry this 3 times
})
.then(function(data) {
    return waitForMessage(data);
})
.catch (function(err) {
   // I don't like catch alls :/ Consider using `.error` instead.
});

I just released https://github.com/zyklus/promise-repeat, which retries a promise until it either times out or a maximum number of attempts are hit. It allows you to write:

receiveMessageAsync(params)
...
.spread(retry(
    function(response, data) {
        return sendResponseAsync(response);
    }
))
...

本文标签: nodejsHow can you retry after an exception in Javascript when using promisesStack Overflow