diff --git a/lib/web/fetch/util.js b/lib/web/fetch/util.js index d71126ca883..5f966eeadff 100644 --- a/lib/web/fetch/util.js +++ b/lib/web/fetch/util.js @@ -1021,39 +1021,37 @@ function isomorphicEncode (input) { * @param {ReadableStream>} reader * @param {(bytes: Uint8Array) => void} successSteps * @param {(error: Error) => void} failureSteps - * @returns {Promise} + * @returns {void} */ -async function readAllBytes (reader, successSteps, failureSteps) { - try { - const bytes = [] - let byteLength = 0 - - do { - const { done, value: chunk } = await reader.read() - - if (done) { - // 1. Call successSteps with bytes. - successSteps(Buffer.concat(bytes, byteLength)) - return - } +function readAllBytes (reader, successSteps, failureSteps) { + const bytes = [] + let byteLength = 0 + + const readResultHandler = ({ done, value: chunk }) => { + // close steps + if (done) { + // 1. Call successSteps with bytes. + successSteps(Buffer.concat(bytes, byteLength)) + return + } - // 1. If chunk is not a Uint8Array object, call failureSteps - // with a TypeError and abort these steps. - if (!isUint8Array(chunk)) { - failureSteps(new TypeError('Received non-Uint8Array chunk')) - return - } + // chunk steps, given chunk + // 1. If chunk is not a Uint8Array object, call failureSteps + // with a TypeError and abort these steps. + if (!isUint8Array(chunk)) { + failureSteps(new TypeError('Received non-Uint8Array chunk')) + return + } - // 2. Append the bytes represented by chunk to bytes. - bytes.push(chunk) - byteLength += chunk.length + // 2. Append the bytes represented by chunk to bytes. + bytes.push(chunk) + byteLength += chunk.length - // 3. Read-loop given reader, bytes, successSteps, and failureSteps. - } while (true) - } catch (e) { - // 1. Call failureSteps with e. - failureSteps(e) + // 3. Read-loop given reader, bytes, successSteps, and failureSteps. + reader.read().then(readResultHandler, failureSteps) } + + reader.read().then(readResultHandler, failureSteps) } /**