@@ -254,13 +254,17 @@ async function fetch(url, options) {
254
254
}
255
255
}
256
256
257
- async function getPendingFetch ( ) {
257
+ function isFetchPending ( ) {
258
+ return ! ! pendingFetches [ url ] ;
259
+ }
260
+
261
+ async function pendingFetchIsOver ( ) {
258
262
if ( pendingFetches [ url ] ) {
259
263
log ( 'wait for pending request' ) ;
260
- return pendingFetches [ url ] . promise ;
264
+ await pendingFetches [ url ] . promise ;
261
265
}
262
266
else {
263
- return false ;
267
+ throw new Error ( 'There was no pending request' ) ;
264
268
}
265
269
}
266
270
@@ -269,7 +273,7 @@ async function fetch(url, options) {
269
273
* the code may resolve or reject it through calls to resolvePendingFetch and
270
274
* rejectPendingFetch functions
271
275
*/
272
- function addPendingFetch ( url ) {
276
+ function addPendingFetch ( ) {
273
277
let resolve = null ;
274
278
let reject = null ;
275
279
let promise = new Promise ( ( innerResolve , innerReject ) => {
@@ -284,13 +288,13 @@ async function fetch(url, options) {
284
288
promise . catch ( err => { } ) ;
285
289
}
286
290
287
- function resolvePendingFetch ( url ) {
291
+ function resolvePendingFetch ( ) {
288
292
if ( ! pendingFetches [ url ] ) return ;
289
293
pendingFetches [ url ] . resolve ( true ) ;
290
294
delete pendingFetches [ url ] ;
291
295
}
292
296
293
- function rejectPendingFetch ( url , err ) {
297
+ function rejectPendingFetch ( err ) {
294
298
if ( ! pendingFetches [ url ] ) return ;
295
299
pendingFetches [ url ] . reject ( err ) ;
296
300
delete pendingFetches [ url ] ;
@@ -396,30 +400,31 @@ async function fetch(url, options) {
396
400
397
401
log ( 'fetch ' + url ) ;
398
402
await checkCacheFolder ( ) ;
399
- let pendingFetchWasOngoing = await getPendingFetch ( ) ;
400
- if ( pendingFetchWasOngoing ) {
401
- // There was a pending fetch, reuse the cached answer
402
- // (NB: we would not be able to reuse the Response object directly because
403
+ if ( isFetchPending ( ) ) {
404
+ // There is a pending fetch, wait for that fetch to finish and reuse the
405
+ // cached answer (NB: we cannot reuse the Response object directly because
403
406
// response body stream can only be read once)
407
+ let pendingFetch = await pendingFetchIsOver ( ) ;
408
+
404
409
log ( 'pending request over, return response from cache' ) ;
405
410
return readFromCache ( ) ;
406
411
}
407
412
else {
408
- addPendingFetch ( url ) ;
413
+ addPendingFetch ( ) ;
409
414
try {
410
415
let headers = await readHeadersFromCache ( ) ;
411
416
if ( hasExpired ( headers , config . refresh ) ) {
412
417
let response = await conditionalFetch ( headers ) ;
413
- resolvePendingFetch ( url ) ;
418
+ resolvePendingFetch ( ) ;
414
419
return response ;
415
420
}
416
421
else {
417
- resolvePendingFetch ( url , 'test' ) ;
422
+ resolvePendingFetch ( ) ;
418
423
return readFromCache ( ) ;
419
424
}
420
425
}
421
426
catch ( err ) {
422
- rejectPendingFetch ( url , err ) ;
427
+ rejectPendingFetch ( err ) ;
423
428
throw err ;
424
429
}
425
430
}
0 commit comments