Understanding promise.race() usage

后端 未结 6 1488
隐瞒了意图╮
隐瞒了意图╮ 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:05

    Here's an easy example to understand the use of promise.race():

    Imagine you need to fetch some data from a server and if the data takes too long to load (say 15 seconds) you want to show an error.

    You would call promise.race() with two promises, the first being your ajax request and the second being a simple setTimeout(() => resolve("ERROR"), 15000)

提交回复
热议问题