@@ -113,18 +113,13 @@ export default isFetchSupported && (async (config) => {
113113
114114 responseType = responseType ? ( responseType + '' ) . toLowerCase ( ) : 'text' ;
115115
116- let [ composedSignal , stopTimeout ] = ( signal || cancelToken || timeout ) ?
117- composeSignals ( [ signal , cancelToken ] , timeout ) : [ ] ;
116+ let composedSignal = composeSignals ( [ signal , cancelToken && cancelToken . toAbortSignal ( ) ] , timeout ) ;
118117
119- let finished , request ;
118+ let request ;
120119
121- const onFinish = ( ) => {
122- ! finished && setTimeout ( ( ) => {
123- composedSignal && composedSignal . unsubscribe ( ) ;
124- } ) ;
125-
126- finished = true ;
127- }
120+ const unsubscribe = composedSignal && composedSignal . unsubscribe && ( ( ) => {
121+ composedSignal . unsubscribe ( ) ;
122+ } ) ;
128123
129124 let requestContentLength ;
130125
@@ -161,7 +156,7 @@ export default isFetchSupported && (async (config) => {
161156
162157 // Cloudflare Workers throws when credentials are defined
163158 // see https://github.com/cloudflare/workerd/issues/902
164- const isCredentialsSupported = "credentials" in Request . prototype ;
159+ const isCredentialsSupported = "credentials" in Request . prototype ;
165160 request = new Request ( url , {
166161 ...fetchOptions ,
167162 signal : composedSignal ,
@@ -176,7 +171,7 @@ export default isFetchSupported && (async (config) => {
176171
177172 const isStreamResponse = supportsResponseStream && ( responseType === 'stream' || responseType === 'response' ) ;
178173
179- if ( supportsResponseStream && ( onDownloadProgress || isStreamResponse ) ) {
174+ if ( supportsResponseStream && ( onDownloadProgress || ( isStreamResponse && unsubscribe ) ) ) {
180175 const options = { } ;
181176
182177 [ 'status' , 'statusText' , 'headers' ] . forEach ( prop => {
@@ -193,7 +188,7 @@ export default isFetchSupported && (async (config) => {
193188 response = new Response (
194189 trackStream ( response . body , DEFAULT_CHUNK_SIZE , onProgress , ( ) => {
195190 flush && flush ( ) ;
196- isStreamResponse && onFinish ( ) ;
191+ unsubscribe && unsubscribe ( ) ;
197192 } , encodeText ) ,
198193 options
199194 ) ;
@@ -203,9 +198,7 @@ export default isFetchSupported && (async (config) => {
203198
204199 let responseData = await resolvers [ utils . findKey ( resolvers , responseType ) || 'text' ] ( response , config ) ;
205200
206- ! isStreamResponse && onFinish ( ) ;
207-
208- stopTimeout && stopTimeout ( ) ;
201+ ! isStreamResponse && unsubscribe && unsubscribe ( ) ;
209202
210203 return await new Promise ( ( resolve , reject ) => {
211204 settle ( resolve , reject , {
@@ -218,7 +211,7 @@ export default isFetchSupported && (async (config) => {
218211 } )
219212 } )
220213 } catch ( err ) {
221- onFinish ( ) ;
214+ unsubscribe && unsubscribe ( ) ;
222215
223216 if ( err && err . name === 'TypeError' && / f e t c h / i. test ( err . message ) ) {
224217 throw Object . assign (
0 commit comments