const array = [1, 2, 3]
const wrapper = (e) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
if(e === 2) {
reject(e+":rejected")
} else {
console.log(e+":resolved")
resolve()
}
}, e * 1000);
})
}
const main = async() => {
try {
await Promise.all(array.map(e => wrapper(e)))
} catch(e){
console.log(e)
}
}
main()
Current output:
1:resolved
2:rejected
3:resolved
Expected output:
1:resolved
2:rejected
How can I cancel the unresolved promises when one promise is rejected. In this case, I'm going to cancel the promise 3
for(let i = 1;i <= 3;i ++) {
await wrapper(i)
}
This is not a solution, hence I'd like to run functions at one time like multiple thread not one by one. That's why I used promise.all