When to use promise.all()?

前端 未结 8 831
孤街浪徒
孤街浪徒 2020-12-02 17:27

This is more of a conceptual question. I understand the Promise design pattern, but couldn\'t find a reliable source to answer my question about promise.all():<

8条回答
  •  日久生厌
    2020-12-02 17:47

    I use promise.all() when I have to do some requests to my API and I don't want to display something before the application loads all the data requested, so I delay the execution flow until I have all the data I need.

    Example:

    What I want to do I want to load the users of my app and their products (imagine that you have to do multiple requests) before displaying a table in my app with the user emails and the product names of each user.

    What I do next I send the requests to my API creating the promises and using promise.all()

    What I do when all the data has been loaded Once the data arrives to my app, I can execute the callback of promises.all() and then make visible the table with the users.

    I hope it helps you to see in which scenario makes sense to use promises.all()

提交回复
热议问题