Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

实现带定时器的异步队列 #48

Open
Inchill opened this issue Nov 1, 2022 · 0 comments
Open

实现带定时器的异步队列 #48

Inchill opened this issue Nov 1, 2022 · 0 comments

Comments

@Inchill
Copy link
Owner

Inchill commented Nov 1, 2022

class Queue {
  constructor () {
    this.tasks = []
  }

  task (time, fn) {
    this.tasks.push({ fn, time })
    return this
  }

  async start () {
    for (const item of this.tasks) {
      const { fn, time } = item
      await this.timeout(time).then(fn)
    }
  }

  timeout (time) {
    return new Promise(resolve => setTimeout(resolve, time))
  }
}

new Queue()
  .task(1000, () => console.log(111))
  .task(2000, () => console.log(333))
  .task(1000, () => console.log(444))
  .start()
@Inchill Inchill changed the title 实现简单的异步队列 实现带定时器的异步队列 Nov 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant