promise.all() method in javascript promise
The Promise.all()
method takes an iterable of promises as an input, and returns a single Promise
that resolves to an array of the results of the input promises. This returned promise will fulfill when all of the input's promises have fulfilled, or if the input iterable contains no promises. It rejects immediately upon any of the input promises rejecting or non-promises throwing an error, and will reject with this first rejection message / error.
const promise1 = () => new Promise((resolve,reject) => {
setTimeout(() => {
resolve(1)
},100)
})
const promise2 = () => new Promise((resolve,reject) => {
resolve(2)
})
const promise3 = () => new Promise((resolve,reject) => {
setTimeout(() => {
resolve(3)
})
})
Promise.all([promise1(),promise2(),promise3()])
.then((val) => {
console.log(val) // [1,2,3]
})
.catch((err) => {
console.log(err)
})
Comments
Post a Comment