Skip to content

Commit 1a9a4c0

Browse files
authored
fix: keep promise chains intact (#4558)
1 parent d23bf9c commit 1a9a4c0

File tree

6 files changed

+18
-25
lines changed

6 files changed

+18
-25
lines changed

lib/core/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,14 +615,14 @@ function ReadableStreamFrom (iterable) {
615615
pull (controller) {
616616
return iterator.next().then(({ done, value }) => {
617617
if (done) {
618-
queueMicrotask(() => {
618+
return queueMicrotask(() => {
619619
controller.close()
620620
controller.byobRequest?.respond(0)
621621
})
622622
} else {
623623
const buf = Buffer.isBuffer(value) ? value : Buffer.from(value)
624624
if (buf.byteLength) {
625-
controller.enqueue(new Uint8Array(buf))
625+
return controller.enqueue(new Uint8Array(buf))
626626
} else {
627627
return this.pull(controller)
628628
}

lib/dispatcher/dispatcher-base.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ class DispatcherBase extends Dispatcher {
7878
// Should not error.
7979
this[kClose]()
8080
.then(() => this.destroy())
81-
.then(() => {
82-
queueMicrotask(onClosed)
83-
})
81+
.then(() => queueMicrotask(onClosed))
8482
}
8583

8684
destroy (err, callback) {
@@ -127,9 +125,8 @@ class DispatcherBase extends Dispatcher {
127125
}
128126

129127
// Should not error.
130-
this[kDestroy](err).then(() => {
131-
queueMicrotask(onDestroyed)
132-
})
128+
this[kDestroy](err)
129+
.then(() => queueMicrotask(onDestroyed))
133130
}
134131

135132
dispatch (opts, handler) {

lib/dispatcher/pool-base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class PoolBase extends DispatcherBase {
5252
for (let i = 0; i < this[kClients].length; i++) {
5353
closeAll[i] = this[kClients][i].close()
5454
}
55-
Promise.all(closeAll)
55+
return Promise.all(closeAll)
5656
.then(this[kClosedResolve])
5757
}
5858
}

lib/interceptor/cache.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const CacheRevalidationHandler = require('../handler/cache-revalidation-handler'
99
const { assertCacheStore, assertCacheMethods, makeCacheKey, normalizeHeaders, parseCacheControlHeader } = require('../util/cache.js')
1010
const { AbortError } = require('../core/errors.js')
1111

12+
const nop = () => {}
13+
1214
/**
1315
* @typedef {(options: import('../../types/dispatcher.d.ts').default.DispatchOptions, handler: import('../../types/dispatcher.d.ts').default.DispatchHandler) => void} DispatchFn
1416
*/
@@ -102,7 +104,7 @@ function handleUncachedResponse (
102104
}
103105

104106
if (typeof handler.onHeaders === 'function') {
105-
handler.onHeaders(504, [], () => {}, 'Gateway Timeout')
107+
handler.onHeaders(504, [], nop, 'Gateway Timeout')
106108
if (aborted) {
107109
return
108110
}
@@ -325,7 +327,7 @@ function handleResult (
325327
if (success) {
326328
sendCachedValue(handler, opts, result, age, context, true)
327329
} else if (util.isStream(result.body)) {
328-
result.body.on('error', () => {}).destroy()
330+
result.body.on('error', nop).destroy()
329331
}
330332
},
331333
new CacheHandler(globalOpts, cacheKey, handler),
@@ -336,7 +338,7 @@ function handleResult (
336338

337339
// Dump request body.
338340
if (util.isStream(opts.body)) {
339-
opts.body.on('error', () => {}).destroy()
341+
opts.body.on('error', nop).destroy()
340342
}
341343

342344
sendCachedValue(handler, opts, result, age, null, false)
@@ -405,18 +407,17 @@ module.exports = (opts = {}) => {
405407
const result = store.get(cacheKey)
406408

407409
if (result && typeof result.then === 'function') {
408-
result.then(result => {
409-
handleResult(dispatch,
410+
return result
411+
.then(result => handleResult(dispatch,
410412
globalOpts,
411413
cacheKey,
412414
handler,
413415
opts,
414416
reqCacheControl,
415417
result
416-
)
417-
})
418+
))
418419
} else {
419-
handleResult(
420+
return handleResult(
420421
dispatch,
421422
globalOpts,
422423
cacheKey,
@@ -426,8 +427,6 @@ module.exports = (opts = {}) => {
426427
result
427428
)
428429
}
429-
430-
return true
431430
}
432431
}
433432
}

lib/mock/mock-utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,7 @@ function mockDispatch (opts, handler) {
337337
// synchronously throw the error, which breaks some tests.
338338
// Rather, we wait for the callback to resolve if it is a
339339
// promise, and then re-run handleReply with the new body.
340-
body.then((newData) => handleReply(mockDispatches, newData))
341-
return
340+
return body.then((newData) => handleReply(mockDispatches, newData))
342341
}
343342

344343
const responseData = getResponseData(body)

lib/mock/snapshot-agent.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,9 @@ class SnapshotAgent extends MockAgent {
162162
headers: responseData.headers,
163163
body: responseBody,
164164
trailers: responseData.trailers
165-
}).then(() => {
166-
handler.onResponseEnd(controller, trailers)
167-
}).catch((error) => {
168-
handler.onResponseError(controller, error)
169165
})
166+
.then(() => handler.onResponseEnd(controller, trailers))
167+
.catch((error) => handler.onResponseError(controller, error))
170168
}
171169
}
172170

0 commit comments

Comments
 (0)