使用 Promise 实现请求自动重试
使用 Promise 实现请求自动重试
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-11-20
* @modified
*
* @description
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @time O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
const log = console.log;
const autoRefetch = (url = ``, times = 3) => {
const promise = fetch(url);
promise.then(res => res.json(), err => {
if(times > 0) {
times -= 1;
promise = fetch(url);
}
}).catch(err => {
return Promise.reject(err);
})
return promise;
}
function maxRequest(url = ``, times = 3) {
return new Promise((resolve, reject) => {
if (times === 0) {
reject('max request number')
return
}
Promise.resolve(fetch(url)).then(value => {
log(`OK`)
resolve(value);
}).catch(() => {
log(`Error`)
times -= 1;
return maxRequest(url, times)
})
})
}
// function maxRequest(fn, maxNum) {
// return new Promise((resolve, reject) => {
// if (maxNum === 0) {
// reject('max request number')
// return
// }
// Promise.resolve(fn()).then(value => {
// resolve(value)
// }).catch(() => {
// return maxRequest(fn, maxNum - 1)
// })
// })
// }
模拟 Promise.all & Promise.allSettled
Promise.all
要么全部 promise 结果都成功了,返回全部的 promise 构成的一个结果值的数组;
要么只要有一个 promise 失败了,就返回失败了的 promise 的 error 值,默认 undefined
一句话总结: 全部 promise 结果都成功了,返回一个有所有成功的结果值的数组; 只要有一个promise 失败了,就的返回失败结果;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
PromiseAll = function(promises) {
const values = [];
let count = 0;
return new Promise((resolve, reject) => {
promises.forEach((promise, index) => {
// promise.then ? 强制转换
Promise.resolve(promise).then(res => {
count += 1;
values[index] = res;
if (count === promises.length) {
resolve(values);
}
}, err => {
reject(err);
})
})
})
}
```js
// pending...
> Promise.allSettled 返回全部的 promise 的结果,无论 promise 结果是成功还是失败,构成一个可迭代的数组对象
成功的 promise 返回一个有 status: 'fulfilled' 和 value 构成的对象
失败的 promise 返回一个有 status: 'rejected' 和 reason 构成的对象
一句话总结: 无论 promise 是成功了还是失败了, 最终都返回一个有 status 和 value 或 reason 构成的对象数组;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled
```js
PromiseAllSellted = function(promises) {
const result = [];
let count = 0;
return new Promise((resolve, reject) => {
// promise.then ? 强制转换
promises.forEach((promise, index) => {
Promise.resolve(promise).then(res => {
result[index] = {
status: `fullfilled`,
value: res,
}
}, err => {
result[index] = {
status: `rejected`,
reason: err,
}
}).finally(() => {
count++
if (count === promises.length) {
resolve(result)
}
})
})
})
}

Promise.allSettled & Promise.all & Promise.race & Promise.any All In One
https://www.cnblogs.com/xgqfrms/p/13414614.html
refs
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
使用 Promise 实现请求自动重试的更多相关文章
- 精讲RestTemplate第8篇-请求失败自动重试机制
本文是精讲RestTemplate第8篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
- 精讲响应式WebClient第6篇-请求失败自动重试机制,强烈建议你看一看
本文是精讲响应式WebClient第6篇,前篇的blog访问地址如下: 精讲响应式webclient第1篇-响应式非阻塞IO与基础用法 精讲响应式WebClient第2篇-GET请求阻塞与非阻塞调用方 ...
- .NET:遇到并发问题,什么样的情况下需要自动重试?
背景 多用户系统会出现并发问题,应对并发问题的两种方式是“悲观锁”和“乐观锁”,多数情况下都会采用“乐观锁”,这引发了一个问题,如果检查出乐观并发异常如何重试?是让最终用户手工重试?还是让系统自动重试 ...
- MQ消费失败,自动重试思路
在遇到与第三方系统做对接时,MQ无疑是非常好的解决方案(解耦.异步).但是如果引入MQ组件,随之要考虑的问题就变多了,如何保证MQ消息能够正常被业务消费.所以引入MQ消费失败情况下,自动重试功能是非常 ...
- [转载]rabbitmq可靠发送的自动重试机制
转载地址http://www.jianshu.com/p/6579e48d18ae http://www.jianshu.com/p/4112d78a8753 接这篇 在上文中,主要实现了可靠模式的c ...
- rabbitmq 不发送ack消息如何处理:rabbitmq可靠发送的自动重试机制
转载地址:http://www.jianshu.com/p/6579e48d18ae http://www.jianshu.com/p/4112d78a8753 接这篇 在上文中,主要实现了可靠模式的 ...
- axios添加了header信息后发送的get请求自动编程option请求了
axios添加了header信息后发送的get请求自动编程option请求了 webpack 代理转发 Provisional headers are shown 在Vue中如何使用axios跨域访问 ...
- 在ES批量插入数据超时时自动重试
当我们使用ES批量插入数据的时候,一般会这样写代码: from elasticsearch import Elasticsearch,helpers es =Elasticsearch(hosts=[ ...
- Cypress系列(65)- 测试运行失败自动重试
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 重试的介绍 学习前的三问 什么是重试测试 ...
随机推荐
- mail Header Injection Exploit
Preventing Email Header Injection - PHundamental PHP Best Practices - http://nyphp.org/phundamentals ...
- 4、剑指offer——从尾到头打印链表java实现
**题目描述** **输入一个链表,按链表从尾到头的顺序返回一个ArrayList.** 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M 思路: 1.如果链 ...
- 关于MongoDB的简单理解(二)--Java篇
一.声明 本文依赖于 MongoDB JVM DRIVERS 4.1 版本编写. 本文案例依赖于 Maven 项目管理工具. 二.本文主要讲解哪些内容? 如何连接到MongoDB 通过TLS/SSL连 ...
- LOJ10074架设电话线
USACO 2008 Jan. Silver 在郊区有 N 座通信基站,P 条双向电缆,第 i 条电缆连接基站 Ai 和 Bi.特别地,1 号基站是通信公司的总站,N 号基站位于一座农场中.现在, ...
- Tensorflow-卷积神经网络CNN
卷积神经网络CNN 结构 池化操作 手写数字-卷积神经网络实现 import tensorflow as tf from tensorflow.examples.tutorials.mnist imp ...
- Docker Desktop启动Kubernetes
Docker_Desktop启动Kubernetes 参考仓库:https://github.com/AliyunContainerService/k8s-for-docker-desktop 视频参 ...
- 四:SpringBoot-定时任务和异步任务的使用方式
SpringBoot-定时任务和异步任务的使用方式 1.定时任务 2.同步和异步 3.定时器的使用 3.1 定时器执行规则注解 3.2 定义时间打印定时器 3.3 启动类开启定时器注解 4.异步任务 ...
- Phoenix的shell操作
Phoenix的shell操作 一.Phoenix的Shell操作 1.进入(hbase01是主机名,2181是zookeeper的端口) 2.退出(注意结尾不加分号) 3.查询所有表(注意结尾不加分 ...
- Flink-v1.12官方网站翻译-P006-Intro to the DataStream API
DataStream API介绍 本次培训的重点是广泛地介绍DataStream API,使你能够开始编写流媒体应用程序. 哪些数据可以流化? Flink的DataStream APIs for Ja ...
- python中numpy库的一些使用
想不用第三方库实现点深度学习的基础部分,发现numpy真的好难(笑),在此做点遇到的函数的笔记 惯例官方文档:https://docs.scipy.org/doc/numpy-1.16.1/refer ...