I have a list of asynchronous functions. I expected when the very first function resolve (or reject), I can stop others.
For examples:
function wait(ms){
return new Promise(function(resolve){
setTimeout(function(){
console.log('waited ', ms);
resolve();
}, ms);
})
}
promise1 = wait(2000);
promise2 = wait(5000);
listPromise = [promise1, promise2];
Promise.race(listPromise).then(function(){
console.log("Very first promise has resolved!");
})
The actual result is:
> waited 2000
> Very first promise has resolved!
> waited 5000
The expecting result is:
> waited 2000
> Very first promise has resolved!