@@ -30,23 +30,29 @@ const timeout = (promise, ms, abortController) => {
30
30
return promise
31
31
}
32
32
33
- return new Promise ( ( resolve , reject ) => {
34
- const timeoutID = setTimeout ( ( ) => {
35
- reject ( new TimeoutError ( ) )
36
-
37
- abortController . abort ( )
38
- } , ms )
33
+ const start = Date . now ( )
39
34
40
- promise
41
- . then ( ( result ) => {
35
+ return new Promise ( ( resolve , reject ) => {
36
+ const after = ( next ) => {
37
+ return ( res ) => {
42
38
clearTimeout ( timeoutID )
39
+ const time = Date . now ( ) - start
43
40
44
- resolve ( result )
45
- } , ( err ) => {
46
- clearTimeout ( timeoutID )
41
+ if ( time >= ms ) {
42
+ abortController . abort ( )
43
+ reject ( new TimeoutError ( ) )
44
+ return
45
+ }
47
46
48
- reject ( err )
49
- } )
47
+ if ( next ) {
48
+ next ( res )
49
+ }
50
+ }
51
+ }
52
+ const timeoutID = setTimeout ( after ( ) , ms )
53
+
54
+ promise
55
+ . then ( after ( resolve ) , after ( reject ) )
50
56
} )
51
57
}
52
58
@@ -138,7 +144,13 @@ class HTTP {
138
144
opts . headers . set ( 'content-type' , 'application/json' )
139
145
}
140
146
141
- const response = await timeout ( fetch ( url , opts ) , opts . timeout , this . abortController )
147
+ const response = await timeout ( fetch ( url , {
148
+ ...opts ,
149
+
150
+ // node-fetch implements it's own timeout in an addition to the spec so do not
151
+ // pass the timeout value on, otherwise there are two sources of timeout errors
152
+ timeout : undefined
153
+ } ) , opts . timeout , this . abortController )
142
154
143
155
if ( ! response . ok && opts . throwHttpErrors ) {
144
156
if ( opts . handleError ) {
0 commit comments