@a417420427/queue 中文文档教程

发布于 3年前 浏览 23 项目主页 更新于 3年前

an js queue

使用

import Queue from '@a417420427/queue'
const queue = new Queue({ concurrency: 3 })

const sleep = (time: number) =>
  new Promise((resolve) => setTimeout(resolve, time))

const currentTime = () => Date.now()
let timeStamp = currentTime()
queue.push(async () => {
  await sleep(1000)
  console.log('sleep 1000ms after ' + (currentTime() - timeStamp))
})
queue.push(async () => {
  await sleep(2000)
  console.log('sleep 2000ms after ' + (currentTime() - timeStamp))
})
queue.push(async () => {
  await sleep(3000)
  console.log('sleep 3000ms after ' + (currentTime() - timeStamp))
})
queue.push(() => {
  console.log('worked after ' + (currentTime() - timeStamp))
})
queue.push(async () => {
  await sleep(1000)
  console.log('over after ' + (currentTime() - timeStamp))
})

queue.start()

// 输出
/**
sleep 1000ms after 1037
worked after 1037
sleep 2000ms after 2100
over after 2100
sleep 3000ms after 4184
 * 
*/
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文