xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

使用 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 发布文章使用:只允许注册用户才可以访问!


原文地址:https://www.cnblogs.com/xgqfrms/p/14016391.html