@@ -179,6 +179,26 @@ export class HttpAdapter implements AdapterInterface {
179179 data : { } ,
180180 } ;
181181
182+ let cancelListener ;
183+
184+ if ( config . cancelToken ) {
185+ cancelListener = config . cancelToken . once ( 'cancel' , ( reason ) => {
186+ // do nothing if promise is resolved by system
187+ if ( reason && reason . message === CANCEL_CLEAR ) {
188+ return ;
189+ }
190+
191+ /* istanbul ignore next: if request is done cancel token should not throw any error */
192+ if ( req ) {
193+ req . abort ( ) ;
194+ req = null ;
195+ }
196+
197+ debug ( 'Request canceled by user %s, config: %O' , reason , config ) ;
198+ return reject ( new FsRequestError ( `Request aborted. Reason: ${ reason } ` , config , null , FsRequestErrorCode . ABORTED ) ) ;
199+ } ) ;
200+ }
201+
182202 // we need to follow redirect so make same request with new location
183203 if ( [ 301 , 302 ] . indexOf ( res . statusCode ) > - 1 ) {
184204 debug ( 'Redirect received %s' , res . statusCode ) ;
@@ -208,7 +228,7 @@ export class HttpAdapter implements AdapterInterface {
208228
209229 // clear cancel token to avoid memory leak
210230 if ( config . cancelToken ) {
211- config . cancelToken . cancel ( CANCEL_CLEAR ) ;
231+ config . cancelToken . removeListener ( cancelListener ) ;
212232 }
213233
214234 return resolve ( this . request ( Object . assign ( { } , config , { url } ) ) ) ;
@@ -261,28 +281,6 @@ export class HttpAdapter implements AdapterInterface {
261281 } ) ;
262282 } ) ;
263283
264- if ( config . cancelToken ) {
265- config . cancelToken
266- . getSource ( )
267- . then ( reason => {
268- // do nothing if promise is resolved by system
269- if ( reason && reason . message === CANCEL_CLEAR ) {
270- return ;
271- }
272-
273- /* istanbul ignore next: if request is done cancel token should not throw any error */
274- if ( req ) {
275- req . abort ( ) ;
276- req = null ;
277- }
278-
279- debug ( 'Request canceled by user %s, config: %O' , reason , config ) ;
280- return reject ( new FsRequestError ( `Request aborted. Reason: ${ reason } ` , config , null , FsRequestErrorCode . ABORTED ) ) ;
281- } )
282- /* istanbul ignore next: only for safety */
283- . catch ( ( ) => { /* empty */ } ) ;
284- }
285-
286284 if ( config . timeout ) {
287285 req . setTimeout ( config . timeout , ( ) => {
288286 req . abort ( ) ;
0 commit comments