The code below is a just simple example.
function delay(time) {
return new Promise(function (resolve) {
setTimeout(resolve, time);
}).then((e) => {
console.log(`Wait ${time}ms.`);
return time;
});
}
async function main() {
let r = await Promise.race([
delay(1000),
delay(2000),
]);
console.log(r);
}
await main();
Output:
Wait 1000ms.
1000
Wait 2000ms.
I want to stop the remaining code except the code that was executed first through Promise.race.
Is there any other way?