Using Promise.all to resolve multiple promises
So far, we've seen how to use promises to perform asynchronous operations in sequence. This is useful when the individual steps are long-running operations. However, this might not always be the more efficient configuration. Quite often, we can perform multiple asynchronous operations at the same time.
In this recipe, we'll see how to use Promise.all to start multiple asynchronous operations, without waiting for the previous one to complete.
Getting ready
This recipe assumes that you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
3-06-using-promise-all. - Copy or create an
index.htmlthat loads and runs amainfunction frommain.js.
- Create a
main.jsfile that creates an object namedrocket, and callsPromise.allwith an empty array as the first argument:
export...