I found this code in a project:
const fn = async () => {
let x = 0;
for(let i = 0; i < 50; i++){
const res = await api.call(i);
if(res.someProp) x++;
}
return x;
}
I want to be able to stop it mid way, so that if I call it again, it will start from scratch and discard the previous call results. To avoid making two sets of requests at the same time.
break;out of a for loop, so what you can do is check a global flag variable (likestopLoop) inside the loop, and break if it's true. If you want the loop to start over, you can also reset x and i to 0 inside the loop (andstopLooptofalse).api.callto complete, and the code that originally calledfndecides to give up. How can it cancel the whole chain without explicitly passing in a participatory cancellation object that every api beneath must support. At some point it's likely to be waiting for a low-level async call, that wouldn't participate. So I think the question is asking how to tear down the promise chain and cancel it. (FWIW, I think the answer is you can't, you must add some explicit cancellation handling at the levels that can do so.)