How to wait for 2 or more Promises to resolve in JavaScript

Suppose you need to start 2 or more Promises and wait for their results. How to do it?

Suppose you need to start 2 or morepromiseAnd wait for their results.

After both parties are resolved, you want to continue.

How to do this with JavaScript?

You usePromise.all():

const promise1 = //...
const promise2 = //...

const data = await Promise.all([promise1, promise2])

const dataFromPromise1 = data[0] const dataFromPromise2 = data[1]

If you prefer to use pure promises instead ofAsync/await, Please use the following syntax:

const promise1 = //...
const promise2 = //...

Promise.all([promise1, promise2]).then(data => {
	const dataFromPromise1 = data[0]
	const dataFromPromise2 = data[1]
})

Download mine for freeJavaScript beginner's manual


More js tutorials: