@@ -39,10 +39,11 @@ function isResponseTimedOut(retryOptions) {
39
39
* @param {RetryOptions } retryOptions whether or not to retry on all http error codes or just >500
40
40
* @param {Object } error error object if the fetch request returned an error
41
41
* @param {Object } response fetch call response
42
+ * @param {Number } wait Amount of time we will wait before retrying next
42
43
* @returns {Boolean } whether or not to retry the request
43
44
*/
44
- function shouldRetry ( retryOptions , error , response ) {
45
- if ( isResponseTimedOut ( retryOptions ) ) {
45
+ function shouldRetry ( retryOptions , error , response , waitTime ) {
46
+ if ( getTimeRemaining ( retryOptions ) < waitTime ) {
46
47
return false ;
47
48
} else if ( retryOptions && retryOptions . retryOnHttpResponse ) {
48
49
return retryOptions . retryOnHttpResponse ( response ) ;
@@ -166,9 +167,9 @@ module.exports = async function (url, options) {
166
167
167
168
return new Promise ( function ( resolve , reject ) {
168
169
const wrappedFetch = async ( ) => {
169
- let processLastResult = ( ) => { } ;
170
170
while ( ! isResponseTimedOut ( retryOptions ) ) {
171
171
++ attempt ;
172
+ const waitTime = getRetryDelay ( retryOptions ) ;
172
173
173
174
let timeoutHandler ;
174
175
if ( retryOptions . socketTimeout ) {
@@ -178,39 +179,30 @@ module.exports = async function (url, options) {
178
179
}
179
180
180
181
try {
181
- // console.log(`Fetching ${url} attempt ${attempt}`);
182
182
const response = await fetch ( url , options ) ;
183
- processLastResult = ( ) => resolve ( response ) ;
184
183
185
- if ( shouldRetry ( retryOptions , null , response ) ) {
186
- console . error ( `Retrying in ${ retryOptions . retryInitialDelay } milliseconds, attempt ${ attempt } failed (status ${ response . status } ): ${ response . statusText } ` ) ;
184
+ if ( shouldRetry ( retryOptions , null , response , waitTime ) ) {
185
+ console . error ( `Retrying in ${ waitTime } milliseconds, attempt ${ attempt } failed (status ${ response . status } ): ${ response . statusText } ` ) ;
187
186
} else {
188
187
// response.timeout should reflect the actual timeout
189
188
response . timeout = retryOptions . socketTimeout ;
190
189
return resolve ( response ) ;
191
190
}
192
191
} catch ( error ) {
193
- processLastResult = ( ) => reject ( error ) ;
194
192
const response = { status : error . code || 500 , statusMessage : error . message || "Unknown server error" } ;
195
- if ( ! shouldRetry ( retryOptions , error , response ) ) {
193
+ if ( ! shouldRetry ( retryOptions , error , response , waitTime ) ) {
196
194
if ( error . name === 'AbortError' ) {
197
195
return reject ( new FetchError ( `network timeout at ${ url } ` , 'request-timeout' ) ) ;
198
196
} else {
199
197
return reject ( error ) ;
200
198
}
201
199
}
202
- console . error ( `Retrying in ${ retryOptions . retryInitialDelay } milliseconds, attempt ${ attempt } error: ${ error . message } ` ) ;
200
+ console . error ( `Retrying in ${ waitTime } milliseconds, attempt ${ attempt } error: ${ error . message } ` ) ;
203
201
} finally {
204
202
clearTimeout ( timeoutHandler ) ;
205
203
}
206
-
207
204
// Fetch loop is about to repeat, delay as needed first.
208
- const waitTime = getRetryDelay ( retryOptions ) ;
209
- if ( getTimeRemaining ( retryOptions ) < waitTime ) {
210
- console . error ( `Timeout during retry delay, returning last received response` ) ;
211
- processLastResult ( ) ;
212
- break ;
213
- } else if ( waitTime > 0 ) {
205
+ if ( waitTime > 0 ) {
214
206
await new Promise ( resolve => setTimeout ( resolve , waitTime ) ) ;
215
207
}
216
208
retryOptions . retryInitialDelay *= retryOptions . retryBackoff ; // update retry interval
0 commit comments