请求串行问题

题目描述

创建一个createFlow方法,方法接受一个二维数组函数,其中函数可以是同步,也可以是异步,甚至可以是嵌套Flow和数组,要求按照数组顺序输出结果

方法一:function

js
const sleep = (timer) => new Promise(resolve => setTimeout(resolve, timer))

const createFlow = (arr = []) => {
  // 串行,扁平化
  const requestList = [...arr].flat()
  const run = async (cb) => {
    for (let i = 0; i < requestList.length; i += 1) {
      const task = requestList[i]
      if (typeof task === 'function') {
        await task()
      } else if (task && task.isFlow) {
        await task.run()
      }
    }
    cb && cb()
  }
  return {
    run,
    // 支持嵌套
    isFlow: true
  }
}

createFlow([
  () => console.log(1),
  () => sleep(400).then(() => console.log(2)),
  [
    () => console.log(3),
    () => sleep(300).then(() => console.log(4))
  ],
  createFlow([
    () => console.log(5),
    () => sleep(200).then(() => console.log(6))
  ])
]).run(() => {
  console.log(7)
})

方法二:class

class 可以用 instanceof 来判断是否是嵌套

js
class Flow {
  constructor(arr) {
    this.queue = [...arr.flat()]
  }

  async run(cb) {
    for (let task of this.queue) {
      if (typeof task === 'function') {
        await task()
      } else if (task instanceof Flow) {
        await task.run()
      }
    }
    cb && cb()
  }
}

const createFlow = (arr) => new Flow(arr)

createFlow([
  () => console.log(1),
  () => sleep(400).then(() => console.log(2)),
  [
    () => console.log(3),
    () => sleep(300).then(() => console.log(4))
  ],
  createFlow([
    () => console.log(5),
    () => sleep(200).then(() => console.log(6))
  ])
]).run(() => {
  console.log(7)
})

沿着联系继续阅读

0 条反向链接

引用本文

还没有公开笔记引用这里。

本文提到

暂时没有其他关联。

在花园里找一找

输入关键词开始搜索。按 Esc 关闭。