1

可以使用 Promise 和异步函数。手动实现一个同步队列,实现代码如下:

class SyncQueue {
  constructor(maxConcurrency = 3) {
    this.maxConcurrency = maxConcurrency // 最大并发请求数量
    this.queue = [] // 等待请求的请求队列
    this.running = 0 // 正在执行的请求数量
  }

  enqueue(task) {
    const promise = new Promise((resolve, reject) => {
      this.queue.push({ task, resolve, reject })
      this.processQueue()
    })
    return promise
  }

  processQueue() {
    while (this.queue.length && this.running < this.maxConcurrency) {
      const { task, resolve, reject } = this.queue.shift()
      this.running++
      task()
        .then((result) => {
          this.running--
          resolve(result)
          this.processQueue()
        })
        .catch((error) => {
          this.running--
          reject(error)
          this.processQueue()
        })
    }
  }
  clearQueue() {
    this.queue = []
  }
}

// 示例任务函数
function fetchTask(url) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log(`Fetching from ${url}`)
      resolve(`Data from ${url}`)
    }, Math.random() * 2000) // 模拟网络延迟
  })
}

// 创建一个同步队列实例
const syncQueue = new SyncQueue()

// 向队列中添加任务
syncQueue.enqueue(() => fetchTask('http://example.com/1'))
syncQueue.enqueue(() => fetchTask('http://example.com/2'))
syncQueue.enqueue(() => fetchTask('http://example.com/3'))
syncQueue.enqueue(() => fetchTask('http://example.com/4'))
syncQueue.enqueue(() => fetchTask('http://example.com/5'))
syncQueue.enqueue(() => fetchTask('http://example.com/6'))
syncQueue.enqueue(() => fetchTask('http://example.com/7'))
syncQueue.enqueue(() => fetchTask('http://example.com/8'))
syncQueue.enqueue(() => fetchTask('http://example.com/9'))
syncQueue.enqueue(() => fetchTask('http://example.com/10'))
syncQueue.enqueue(() => fetchTask('http://example.com/11'))
syncQueue.enqueue(() => fetchTask('http://example.com/12'))

// 处理完成后的回调
syncQueue
  .enqueue(() => fetchTask('http://example.com/6'))
  .then((data) => {
    console.log('All tasks completed:', data)
  })

通过这种方式,可以确保任何时刻都有最多三个(或者n个)请求在并发执行,其余的请求将被排队等待。这种同步队列对于控制并发请求的数量非常有用,尤其是在处理可能有大量并发请求的场景。


fuGUI
1.6k 声望1.9k 粉丝

The best time to plant a tree is ten years ago, and the second,let us start