欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS 中實現(xiàn)一個串型異步函數(shù)隊列

 更新時間:2022年07月26日 09:18:02   作者:? Cyber_Bear?  
這篇文章主要介紹了JS 中實現(xiàn)一個串型異步函數(shù)隊列,文章通過async/await 串型請求展開詳情,具有一定的參考價值,需要的朋友可以參考一下

背景

在日常業(yè)務開發(fā)中,總會遇到這種場景,有一串請求,下一個請求依賴上一個異步請求的結果,一般來說,我在處理這種需求的時候使用 async/await 。但是在某些情況(比如面試),可能需要提供更加高級的解決方案。

通常解法

async/await 串型請求

// 生成異步請求函數(shù)
/* 
@param {boolean} isSuccess 是否成功
@param {number} delay 請求返回時間
@return {Promise} promise 
*/
function genAsyncTask(isSuccess, delay) {
  return (params) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        isSuccess ? resolve(params || 'success') : reject('error')
      }, delay)
    })
  }
}
// 異步請求
const asyncTask1 = genAsyncTask(true, 1000)
const asyncTask2 = genAsyncTask(true, 2000)
const asyncTask3 = genAsyncTask(true, 3000)


// 通常解法
;(async()=>{
let result1 = await asyncTask1('a')
let result2 = await asyncTask2(result1)
await asyncTask3(result2)
// ...
})()

for...of 解法

不依賴上次結果,只要求順序執(zhí)行時,注意和Promise.all()區(qū)別

const asyncTasks = [asyncTask3, asyncTask2, asyncTask1]
;(async()=>{
  for(task of asyncTasks){
    console.log(await task())
  }
})()
// 輸出:success    3s之后
// 輸出:success    再2s之后
// 輸出:success    再1s之后

需求一

  • 實現(xiàn) queuePromsies 方法
  • 入?yún)楫惒秸埱蠛瘮?shù)數(shù)組
  • 上個結果作為下個異步函數(shù)的入?yún)?/li>

reduce 實現(xiàn)

/* reduce 
@param {array} promises 異步請求函數(shù)數(shù)組 note: 傳入異步函數(shù),它返回 promise。而不是傳入 promise 對象。 
@initInput (any) initInput 初始參數(shù),作為 reduce 初始值參數(shù)
*/

function queuePromises(asyncTasks, initInput = 'initInput') {
  return asyncTasks.reduce(
    (preAsyncTask, curAsyncTask) =>
      preAsyncTask.then((result) => {
        return curAsyncTask(result)
      }),
    Promise.resolve(initInput)
  )
}

/* 測試用例 */

;(async () => {
  try {
    let result = await queuePromises([asyncTask1, asyncTask2, asyncTask3])
    console.log(result)
  } catch (error) {
    console.log('error', error)
  }
})()
// 輸出:initInput

需求二

  • 實現(xiàn) chainPromise 函數(shù)
  • 不使用 async / await 語法
  • 實現(xiàn)單個 promise 失敗后重新執(zhí)行的配置,即 promise 失敗后允許繼續(xù)嘗試發(fā)送幾次請求,重傳次數(shù)用完則認為失敗,進入 catch

遞歸實現(xiàn)

// 遞歸解法
function chainPromise(tasks, { limit } = { limit: 3 }) {
  const results = []
  let tryTimes = 1
  let index = 0

  function next() {
    const task = tasks[index]
    if (task) {
      return task().then(
        (result) => {
          tryTimes = 1
          index++
          results.push(result)
          return next()
        },
        (reason) => {
          if (tryTimes <= limit) {
            console.log('try!!!', tryTimes)
            tryTimes++
            return next()
          } else {
            throw new Error('limit more than 3')
          }
        }
      )
    } else {
      return Promise.resolve(results)
    }
  }
  return next()
}
chainPromise([asyncTask1, asyncTask2, asyncTask3]), {limit:3}]).then(console.log)
// [1000,2000,3000]

到此這篇關于JS 中實現(xiàn)一個串型異步函數(shù)隊列的文章就介紹到這了,更多相關JS串型異步函數(shù)隊列內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論