Add ability to abort a kfetch call.#20700
Conversation
💔 Build Failed |
src/ui/public/kfetch/index.js
Outdated
| fetching, | ||
| abort, | ||
| }; | ||
| } |
There was a problem hiding this comment.
What about attaching abort to the promise like:
fetching.abort = abort;That way we can also get rid of isAbortable since all requests become abortable and we don't need a secondary api (for abortable requests).
There was a problem hiding this comment.
I originally considered this approach but I had a few concerns about monkey-patching a native API:
-
If a native
abortmethod is introduced to promises at some point in the future then we'll need to ensure it either has identical functionality, or make a decision as to whether to cut over to the official method and ensure BWC or to keep the monkey-patched method and have unexpected behavior. -
Monkey-patching has the potential to confuse consumers as well since this method isn't in the spec, prompting questions like "Is this really a native promise?" and "What other non-spec methods are attached to this promise?" It requires the user to understand the implementation in order to have confidence about what's changed about this promise.
These concerns are mostly based on principle so if you think it's more pragmatic to just monkey-patch the promise, let me know your thoughts because I can definitely be persuaded! OTOH if you think these concerns are worthy then I think creating a secondary API makes sense, because we're highlighting the fact that the consumer is consciously opting into secondary behavior. And realistically, do you think it will be used very often? Do we have many use cases for aborting a fetch?
There was a problem hiding this comment.
These are very good points. I was mostly concerned about having two APIs but as you said, there won't be many use cases for aborting fetch, and in those cases it will be a conscious effort.
I like the approach you suggested with a separate kfetchAbortable method.
|
|
src/ui/public/kfetch/index.js
Outdated
| const abortController = new AbortController(); | ||
| signal = abortController.signal; | ||
| abort = abortController.abort.bind(abortController); | ||
| } |
There was a problem hiding this comment.
Maybe abstract this into a separate createAbortable method that returns { signal, abort }
Thanks for reminding me! Yes I need to do that. |
|
Alternatively, we could also create a method specifically for creating abortable fetch promises. This way we wouldn't need to alter the export function kfetchAbortable(fetchOptions, kibanaOptions) {
const abortController = new AbortController();
const signal = abortController.signal;
const abort = abortController.abort.bind(abortController);
const fetching = kfetch({ ...fetchOptions, signal }, kibanaOptions);
return {
fetching,
abort,
};
} |
|
@nreese @sqren Ready for another look! CC @jen-huang I've implemented the idea we talked about. |
| } | ||
|
|
||
| resolve(res.json()); | ||
| }); |
There was a problem hiding this comment.
We don't need the Promise constructor anymore, do we? Eg. just revert to what we had (unless you prefer this style ofc :) )
There was a problem hiding this comment.
If you don't mind, I would like to keep it in this style because it makes it just a bit more explicit that a promise is being returned (at least to me!)
| fetching, | ||
| abort, | ||
| }; | ||
| } |
nreese
left a comment
There was a problem hiding this comment.
lgtm. very nice and easy to follow
code review
sorenlouv
left a comment
There was a problem hiding this comment.
Looks good. Feel free to merge regardless of the nits.
💔 Build Failed |
💔 Build Failed |
|
Failure: Screenshot: |
|
Retest |
💚 Build Succeeded |
* Split kfetch module into kfetch and kfetchAbortable sub-modules. * Add abortcontroller-polyfill.
|
I really like where this ended up! |

This will be needed for rollups support, since we'll use
kfetchto make the search request to the rollups API endpoint, and courier'scallClientfunction requires the ability to abort the promises representing the in-flight search requests.I tried to add a test asserting that
abortactually stopped the promise from resolving, but I don't think thefetch-mockmodule supports this: