From 3c3fd48a870faa305e2f666431f94f91cd3a468c Mon Sep 17 00:00:00 2001 From: Douglas Brown Date: Tue, 22 Mar 2022 12:25:53 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20Task=20Scoping=20for=20Fet?= =?UTF-8?q?ch=20Cancellation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- polyfill/Fetch.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/polyfill/Fetch.js b/polyfill/Fetch.js index c22fc0bf6..5b164ee86 100644 --- a/polyfill/Fetch.js +++ b/polyfill/Fetch.js @@ -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) @@ -87,8 +89,8 @@ class ReactNativeBlobUtilFetchPolyfill { }; statefulPromise.cancel = () => { cancelHandler = true; - if (task.cancel) - task.cancel(); + if (scopedTask && scopedTask.cancel) + scopedTask.cancel(); }; return statefulPromise;