@@ -57,24 +57,38 @@ class RNFetchBlobFetchPolyfill {
5757 // task is a progress reportable and cancellable Promise, however,
5858 // task.then is not, so we have to extend task.then with progress and
5959 // cancel function
60- let task = promise
60+ let progressHandler , uploadHandler , cancelHandler
61+ let statefulPromise = promise
6162 . then ( ( body ) => {
62- return RNFetchBlob . config ( config )
63- . fetch ( options . method , url , options . headers , body )
63+ let task = RNFetchBlob . config ( config )
64+ . fetch ( options . method , url , options . headers , body )
65+ if ( progressHandler )
66+ task . progress ( progressHandler )
67+ if ( uploadHandler )
68+ task . uploadProgress ( uploadHandler )
69+ if ( cancelHandler )
70+ task . cancel ( )
71+ return task . then ( ( resp ) => {
72+ log . verbose ( 'response' , resp )
73+ // release blob cache created when sending request
74+ if ( blobCache !== null && blobCache instanceof Blob )
75+ blobCache . close ( )
76+ return Promise . resolve ( new RNFetchBlobFetchRepsonse ( resp ) )
77+ } )
6478 } )
6579
66- let statefulPromise = task . then ( ( resp ) => {
67- log . verbose ( 'response' , resp )
68- // release blob cache created when sending request
69- if ( blobCache !== null && blobCache instanceof Blob )
70- blobCache . close ( )
71- return Promise . resolve ( new RNFetchBlobFetchRepsonse ( resp ) )
72- } )
73-
7480 // extend task.then progress with report and cancelling functions
75- statefulPromise . cancel = task . cancel
76- statefulPromise . progress = task . progress
77- statefulPromise . uploadProgress = task . uploadProgress
81+ statefulPromise . progress = ( fn ) => {
82+ progressHandler = fn
83+ }
84+ statefulPromise . uploadProgress = ( fn ) => {
85+ uploadHandler = fn
86+ }
87+ statefulPromise . cancel = ( ) => {
88+ cancelHandler = true
89+ if ( task . cancel )
90+ task . cancel ( )
91+ }
7892
7993 return statefulPromise
8094
0 commit comments