有点不同…
异步重试可以通过建立一条
.catch()链来实现,这与更常见的
.then()链相反。
这种方法是:
- 只有在指定的最大尝试次数下才有可能。(链条必须是有限的长度),
- 仅建议使用较低的最大值。(承诺链消耗的内存与长度成正比)。
否则,请使用递归解决方案。
首先,将一个实用程序函数用作
.catch()回调。
var t = 500;function rejectDelay(reason) { return new Promise(function(resolve, reject) { setTimeout(reject.bind(null, reason), t); });}
现在,您可以非常简洁地构建.catch链:
1.重试直到承诺解决为止
var max = 5;var p = Promise.reject();for(var i=0; i<max; i++) { p = p.catch(attempt).catch(rejectDelay);}p = p.then(processResult).catch(errorHandler);
演示 :https :
//jsfiddle.net/duL0qjqe/
2.重试直到结果达到一定条件,不要拖延
var max = 5;var p = Promise.reject();for(var i=0; i<max; i++) { p = p.catch(attempt).then(test);}p = p.then(processResult).catch(errorHandler);
演示 :https :
//jsfiddle.net/duL0qjqe/1/
3.重试直到结果满足一定条件,然后延迟
考虑到(1)和(2)之后,组合的test + delay同样是微不足道的。
var max = 5;var p = Promise.reject();for(var i=0; i<max; i++) { p = p.catch(attempt).then(test).catch(rejectDelay); // Don't be tempted to simplify this to `p.catch(attempt).then(test, rejectDelay)`. Test failures would not be caught.}p = p.then(processResult).catch(errorHandler);
test()可以是同步或异步的。
添加更多测试也将是微不足道的。只需在两个鱼钩之间夹入一串那么的香肠。
p = p.catch(attempt).then(test1).then(test2).then(test3).catch(rejectDelay);
演示 :https :
//jsfiddle.net/duL0qjqe/3/
所有版本均设计为
attempt可返回承诺的异步功能。也可以想象它返回一个值,在这种情况下,链将遵循其成功路径到达next / terminal
.then()。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)