在使用promises时,如何在Javascript中的异常后重试?

时间:2021-11-07 03:52:33

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

我正在使用Bluebird promise库。我有一系列保证的功能,如下所示:

    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?

偶尔sendMessage会失败,因为,假设要响应的服务器不可用。我希望代码继续尝试永远响应,直到它成功。你不能简单地将sendMessage包装在一个catch中,因为它实际上没有抛出异常,我想,它调用了“error”函数,在这个promisified代码中,它是底部的“catch”。因此必须有一些方法可以在“catch”部分“重试”发送消息。问题是,即使我在“catch”中循环重试,我仍然没有办法跳到promise链并执行剩余的promisified函数。我该如何处理?

EDIT:

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

我对HTTP帖子的重试最终看起来像这样:

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

3 个解决方案

#1


7  

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:

这个想法是你可以包装一个函数,该函数返回一个promise,它会捕获并重试错误,直到重试失败。所以如果你要重试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); 
  });
}

#2


2  

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

#3


0  

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:

我刚刚发布了https://github.com/zyklus/promise-repeat,它会重试一个承诺,直到它超时或达到最大尝试次数。它允许你写:

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

#1


7  

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:

这个想法是你可以包装一个函数,该函数返回一个promise,它会捕获并重试错误,直到重试失败。所以如果你要重试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); 
  });
}

#2


2  

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

#3


0  

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:

我刚刚发布了https://github.com/zyklus/promise-repeat,它会重试一个承诺,直到它超时或达到最大尝试次数。它允许你写:

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