Understanding promise.race() usage

后端 未结 6 1490
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 17:14

As far as I know, there are two options about promise:

  • promise.all()

  • promise.race()

Ok, I know what promise.all()<

6条回答
  •  一整个雨季
    2020-12-09 18:19

    Let's take an sample workaround of Promise.race like below.

    const race = (promises) => {
        return new Promise((resolve, reject) => {
            return promises.forEach(f => f.then(resolve).catch(reject));
        })
    };
    

    You can see race function executes all promises, but whomever finishes first will resolve/reject with wrapper Promise.

提交回复
热议问题