Consider using AbortController.signal to cancel requests
The .cancel() method to cancel requests works great, and it's a great feature to have. It's a popular convention used by many network libraries, however, a convention notheless.
It would be great to add support for an AbortController.signal created outside the generated code so as to use a native browser api rather than a convention.
As an example, react-query has started to create it's own signals in addition to still supporting the .cancel() convention.
@aryzing The only problem here is XHR support, at the moment we rely on a custom handler for this. Maybe we can reuse the AbortController and hook into that.
is there any other possible way to cancel request right now?
@mstosio You can cancel the promises that you get back from the service:
// Save the promise / request that comes out of the service call
const req = SomeService.someCall();
try {
// Wait for result
return await req;
} catch (e) {
// The service can throw two errors (CancelError or ApiError)
if (e instanceof CancelError) {
console.log('Request got cancelled');
}
if (e instanceof ApiError) {
console.log('Request returned with an error');
}
}
// We can cancel the request by canceling the promise
setTimeout(() => {
req.cancel();
}, 1000);
@ferdikoomen my org would be willing to contribute a fix to use AbortController because CancelablePromise is just a convention and it also causes teams to have to adjust their jest tests in an inconvenient way rather than using built-in mockImplementation(async () => { ... }). I just have two questions:
-
Which clients should use AbortController?
- I'm assuming everything except Angular and XHR, based on code observation and your comment above
-
Is it OK that we have an additional condition for this in the handlebar(s) file(s)?
@ferdikoomen Additionally, would it make sense to let consumers provide a custom promise transform function as a short term solution? That way, we can change CancelablePromise to Promise, and not break our test APIs.
This would be great to use with react-query with just being able to pass the signal through.