-
Notifications
You must be signed in to change notification settings - Fork 102
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
Can't handle fetch
cancelation
#230
Comments
Ah, sadly you'll need to keep your own state to handle the race condition here. Something like the following will work: const fetchTask = (url) => task(resolver => {
const controller = new AbortController()
const init = {
signal: controller.signal,
method: 'post',
}
let resolved = false;
let transition = (f) => (x) => { if (!resolved) { resolved = true; f(x); } };
fetch(url, init)
.then(transition(resolver.resolve))
.catch(transition(resolver.reject))
resolver.onCancelled(() => {
controller.abort()
}) I've thought a lot about whether requiring people to explicitly handle these race conditions or having the task resolver just implicitly ignore these calls after a task has moved away from the pending state and I'm still not quite sure what the answer should be---on one hand making it implicit makes things easier, but OTOH it also tends to hide some errors that then become very hard to debug :/ |
Sorry for the long silence... But you suggest is the same. The error occurs due to task can't pass from canceled state to rejected. You can do transition only from
|
the problem is i can't handle cancelled task: const execution = asyncTask() // long execution
.mapRejected((e) => {
console.dir(e) // never get here
})
.run()
setTimeout(() => {
execution.cancel() // cancel after 100 ms
}, 100) how can i handle, that task was cancelled? |
//ApiActionGetWorkshopCancelable :: string -> Task
export const ApiActionGetWorkshopCancelable = workshopId =>
task(resolver => {
const abortController = new AbortController();
API(abortController)
.get(`/workshops/${workshopId}`)
.then(resolver.resolve)
.catch(e => !resolver.isCancelled && resolver.rejected(e));
resolver.onCancelled(() => abortController.abort());
}); Docs: This works for me |
Hi, I can't handle
fetch
cancelation. See code below:Steps to reproduce
Expected behaviour
Task rejected with error
I understand why does it happen but how I can handle cancelation error? Or may be I do it wrong?
The text was updated successfully, but these errors were encountered: