Condition
first two async functions start at the same time
don't know which one finish first
Need to execute a third function on the completion of both of these.
Trying to use promise. Please help achieving it using promise and some other way also if its possible.
something i tried not sure if it's right...
function foop(a){
return new Promise(function(resolve, reject){
window.setTimeout(function(){
console.log("first promise executed");
}, 2000);
window.setTimeout(function(){
console.log("second promise executed");
if(a>5) resolve(a+10);
}, 2000);
if(a<5) reject("error");
});
}
foop(12).then(function(val){
window.setTimeout(function(){
console.log("first then executed -"+ val);
}, 1000);
}).then(function(val){
window.setTimeout(function(){
console.log("second then executed -"+ val);
}, 500);
}).catch(function(err){
console.log("error occured");
});
Expected output order
first promise executed //or second promise executed
second promise executed // or first promise executed
first then executed -22 //only executed after first two async completes in any order
second then executed -22 // execute after the third async completes (first then completes)
error occured // in case of reject