-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how about add a way to interrupt the loop from outside? #7
Comments
I don't see how that could work. |
However, we should improve the usability for this specific case. Braindump: We could maybe add a Which would look like: let loopShouldStop = false;
pForever(async () => {
pForever.endIf(loopShouldStop);
await doJobA();
pForever.endIf(loopShouldStop);
await doJobB();
pForever.endIf(loopShouldStop);
await doJobC();
}); I'm also open to other ideas. |
too may pForever.endIf maybe not elegant enough.🤔 pForever(
[
asyncJobA,
asyncJobB,
async () => {
// some asyn job
},
() => {
// do some sync job
}
],
() => {
// Execute the next task if the condition is true, otherwise, end the loop
return condition;
}
) Also I don't want to wait for the asynchronous task to finish, I want to terminate it immediately.This is a very common requirement in actual development const promise = pForever(async (onCancel) => {
let p;
onCancel(() => p?.cancel());
p = doJobA();
await p;
p = doJobB();
await p;
p = doJobC();
await p;
});
promise.cancel(); |
The proper solution these days is to use AbortController, so it would be something like this if const promise = pForever({signal}, async signal => {
await doJobA({signal});
await doJobA({signal});
await doJobA({signal});
}); |
It would be helpful to have |
Currently only provides a way to break the loop internally by return end symbol, It would be useful to provide a way to cancel the loop from outside like below.
why i need to cancel a promise from outside? bcz code like below is stupid.😓
The text was updated successfully, but these errors were encountered: