I have tried using Promise.race(), but in this case too if one of the Promise is resolved the other Promises keep on running.
Is there a way to stop other processes from running once one of them wins.
I do not want other processes to keep running and using system resources.
I am open to use other ways instead of promises to implement this.
In practice PromiseA is waiting for PromiseZ (I am using the following code to mock similar behaviour)
Since I know now after running PromiseA that PromiseB is not required, I want to stop PromiseB
Is there any better way to do this?
var promiseList = [];
var flag = false;
function PromiseA() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
console.log(flag)
flag = true;
resolve();
}, 2000);
});
}
function PromiseB() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
if (flag === true) {
console.log('checking')
console.log('flag is:' + flag)
resolve();
} else {
reject();
}
}, 7000);
});
}
promiseList.push(PromiseA());
promiseList.push(PromiseB());
Promise.all(promiseList);