Skip to content

实现队列输出 #19

@imondo

Description

@imondo
      // function start(id) {
      //   setTimeout(() => {
      //     execute(id).catch(console.error)
      //   }, id * 1000)
      // }

       function start(id) {
         this.promise = this.promise
           ? this.promise.then(_ => execute(id))
           : execute(id);
       }

      // 测试代码 (请勿更改):

      for (let i = 0; i < 5; i++) {
        start(i);
      }

      function sleep() {
        const duration = Math.floor(Math.random() * 500);
        return new Promise(resolve => setTimeout(resolve, duration));
      }

      function execute(id) {
        return sleep().then(() => {
          console.log("id", id);
        });
      }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions