1. 简易版
  1. class AsyncQueue {
  2. constructor() {
  3. this.queue = []
  4. this.index = 0
  5. }
  6. add(...fn) {
  7. this.queue.push(...fn)
  8. }
  9. run() {
  10. const fn = this.queue[this.index]
  11. fn(this.next.bind(this))
  12. }
  13. next() {
  14. if (this.index >= this.queue.length - 1) return
  15. const fn = this.queue[++this.index]
  16. fn(this.next.bind(this))
  17. }
  18. }

参考资料