From d2c099ce34bd46a002797c57d4bc76ddf0b2b119 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Tue, 22 Mar 2022 17:08:33 +0000 Subject: fix(ext/fetch): Connect async error stack with user code (#13899) --- ext/fetch/26_fetch.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'ext/fetch/26_fetch.js') diff --git a/ext/fetch/26_fetch.js b/ext/fetch/26_fetch.js index 711825985..b13552b02 100644 --- a/ext/fetch/26_fetch.js +++ b/ext/fetch/26_fetch.js @@ -409,8 +409,13 @@ * @param {RequestInit} init */ function fetch(input, init = {}) { + // There is an async dispatch later that causes a stack trace disconnect. + // We reconnect it by assigning the result of that dispatch to `opPromise`, + // awaiting `opPromise` in an inner function also named `fetch()` and + // returning the result from that. + let opPromise = undefined; // 1. - return new Promise((resolve, reject) => { + const result = new Promise((resolve, reject) => { const prefix = "Failed to call 'fetch'"; webidl.requiredArguments(arguments.length, 1, { prefix }); // 2. @@ -441,7 +446,7 @@ } // 12. - PromisePrototypeCatch( + opPromise = PromisePrototypeCatch( PromisePrototypeThen( mainFetch(request, false, requestObject.signal), (response) => { @@ -479,6 +484,14 @@ }, ); }); + if (opPromise) { + PromisePrototypeCatch(result, () => {}); + return (async function fetch() { + await opPromise; + return result; + })(); + } + return result; } function abortFetch(request, responseObject, error) { -- cgit v1.2.3