Skip to content

Commit

Permalink
🐛 Fix Task Scoping for Fetch Cancellation
Browse files Browse the repository at this point in the history
The assigned `task` variable is not available on the cancel method. This
moves the assignment outside the promise scope and allows it to be
correctly called.
  • Loading branch information
sdougbrown committed Mar 22, 2022
1 parent acfb2f0 commit 3c3fd48
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions polyfill/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ class ReactNativeBlobUtilFetchPolyfill {
// task.then is not, so we have to extend task.then with progress and
// cancel function
let progressHandler, uploadHandler, cancelHandler;
let scopedTask = null;
let statefulPromise = promise
.then((body) => {
let task = RNconfig(config)
.fetch(options.method, url, options.headers, body);
scopedTask = task;
if (progressHandler)
task.progress(progressHandler);
if (uploadHandler)
Expand All @@ -87,8 +89,8 @@ class ReactNativeBlobUtilFetchPolyfill {
};
statefulPromise.cancel = () => {
cancelHandler = true;
if (task.cancel)
task.cancel();
if (scopedTask && scopedTask.cancel)
scopedTask.cancel();
};

return statefulPromise;
Expand Down

0 comments on commit 3c3fd48

Please sign in to comment.