现在有一个页面需要在页面初始化前对一批资源进行预请求,请实现预请求函数 preload,要求并发请求数量不超过 5 个,请求失败需要重试,单个资源最多重试 3 次,超出限制时抛出异常,并停止后续请求。 方法定义: preload(urls: string[]) => Promise<void>; 示例: preload([url1, url2, …])   .then(() => {     // 预请求完成,开始初始化页面   })   .catch((error) => {     // 预请求失败   });

区块链毕设网qklbishe.com为您提供问题的解答

现在有一个页面需要在页面初始化前对一批资源进行预请求,请实现预请求函数 preload,要求并发请求数量不超过 5 个,请求失败需要重试,单个资源最多重试 3 次,超出限制时抛出异常,并停止后续请求。
方法定义:

preload(urls: string[]) => Promise<void>;
示例:
preload([url1, url2, …])
  .then(() => {
    // 预请求完成,开始初始化页面
  })
  .catch((error) => {
    // 预请求失败
  });
const ERROR_MSG = {     'ERROR': 'REQUEST_FAIL',     'REDIRECTION': 'REQUEST_REDIRECTION_LOCATION',     'OVERTIME': 'OVER_MAX_RETRY_TIME' }  const axios = (url, resolve, reject) => {     https.get(url, res => {         res.on('end', () => {             const status = res.statusCode             if(status > 300 && status < 400) {                 const location = res.getResponseHeader('Location')                 reject(ERROR_MSG.REDIRECTION, location)             }             resolve()         })     }).on('error', (err) => {         reject(ERROR_MSG.ERROR)     }) }  class Scheduler {     constructor(items, options) {         this.options = options         this.reqPools = []           this.Init(items)     }      Init(items) {         this.handleOptions()         if(!Array.isArray(items)) {             throw new Error("NOT A ARRAY FOR THIS VARIABLE")         }         items.forEach(v => {             this.reqPools.push(new SyncPoolItem(v, this.isRetry, this.maxTryTime))         })     }      handleOptions() {         for (const k in this.options) {             if (Object.hasOwnProperty.call(this.options, k)) {                 const v = this.options[k];                 this[k] = v             }         }     }      run(resolve, rej) {         const maxSync = this['maxSync'] || 5          // Init Container         const container = this.reqPools.splice(0, maxSync)         container.map((v, i) =>              v.run().then(() => {                 return i             }).catch(err => {                 throw new Error(err)             })         )          // 遍历列表         return new Promise(() => {             this.reqPools.reduce((c, r) => {                 return c                 .then(() => {                     return Promise.race(container)                 }).then(i => {                     container[i] = r.run().then(res => {                         return i                     })                 }).catch(err => {                     rej(err)                 })             }, Promise.resolve())             .then(res => {                 Promise.all(container).then(res => {                     resolve()                 }).catch(err => {                     rej(err)                 })             })         })      } }  class SyncPoolItem {     constructor(url, isRetry, maxTryTime) {         this.url = url         this.isRetry = isRetry         this.maxTryTime = maxTryTime         this.currentRetryTime = 1     }      // retry's core     async run() {         // console.log(this.isRetry, this.currentRetryTime, this.maxTryTime, this.url);         if(!this.isRetry || this.currentRetryTime >= this.maxTryTime) {             throw new Error(ERROR_MSG.OVERTIME)         }          try {             return await new Promise((resolve, reject) => {                 axios(this.url, resolve, reject)             })         } catch (err) {             this.currentRetryTime++             this.run()         }     } }   /**  *  @param urls: string[]  */ function preload(urls) {     const reqPools = urls.map(v => v)      return async (options) => {         const scheduler = new Scheduler(reqPools, options)         try {             const res = await new Promise((resolve, reject) => {                 scheduler.run(resolve, reject)                 console.log(2)             })             console.log('res', res)         } catch (err) {             console.log('err', err)         }     } }   preload(urls)({     maxSync: 5,     isRetry: true,     maxTryTime: 3 }).then(res => {     console.log('res_', res); }).catch(err => {     console.log('err_', err); })

51:02

以上就是关于问题现在有一个页面需要在页面初始化前对一批资源进行预请求,请实现预请求函数 preload,要求并发请求数量不超过 5 个,请求失败需要重试,单个资源最多重试 3 次,超出限制时抛出异常,并停止后续请求。
方法定义:
preload(urls: string[]) => Promise<void>;
示例:
preload([url1, url2, …])
  .then(() => {
    // 预请求完成,开始初始化页面
  })
  .catch((error) => {
    // 预请求失败
  });的答案

欢迎关注区块链毕设网-
专业区块链毕业设计成品源码,定制。

区块链NFT链游项目方科学家脚本开发培训

承接区块链项目定制开发

微信:btc9767

QQ :1330797917

TELEGRAM: BTCOK9

承接区块链项目定制开发


qklbishe.com区块链毕设代做网专注|以太坊fabric-计算机|java|毕业设计|代做平台-javagopython毕设 » 现在有一个页面需要在页面初始化前对一批资源进行预请求,请实现预请求函数 preload,要求并发请求数量不超过 5 个,请求失败需要重试,单个资源最多重试 3 次,超出限制时抛出异常,并停止后续请求。 方法定义: preload(urls: string[]) => Promise<void>; 示例: preload([url1, url2, …])   .then(() => {     // 预请求完成,开始初始化页面   })   .catch((error) => {     // 预请求失败   });