From b80e9497f3e8a4100baa0da0cf5486b76f72bf36 Mon Sep 17 00:00:00 2001 From: Khafra Date: Sat, 30 Sep 2023 14:48:36 -0400 Subject: [PATCH] lib: make fetch sync and return a Promise update test PR-URL: https://github.com/nodejs/node/pull/49936 Reviewed-By: Antoine du Hamel Reviewed-By: Benjamin Gruenbaum Reviewed-By: Luigi Pinca Reviewed-By: Chemi Atlow Reviewed-By: LiviaMedeiros Reviewed-By: Yagiz Nizipli Reviewed-By: Minwoo Jung --- lib/internal/process/pre_execution.js | 3 ++- test/parallel/test-fetch.mjs | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/internal/process/pre_execution.js b/lib/internal/process/pre_execution.js index f120f371d9b634..2be86f907bd47c 100644 --- a/lib/internal/process/pre_execution.js +++ b/lib/internal/process/pre_execution.js @@ -292,7 +292,8 @@ function setupUndici() { } if (!getOptionValue('--no-experimental-fetch')) { - async function fetch(input, init = undefined) { + // Fetch is meant to return a Promise, but not be async. + function fetch(input, init = undefined) { return lazyUndici().fetch(input, init); } diff --git a/test/parallel/test-fetch.mjs b/test/parallel/test-fetch.mjs index e24a38eb27db37..bbdb7130ed2324 100644 --- a/test/parallel/test-fetch.mjs +++ b/test/parallel/test-fetch.mjs @@ -10,6 +10,14 @@ assert.strictEqual(typeof globalThis.Headers, 'function'); assert.strictEqual(typeof globalThis.Request, 'function'); assert.strictEqual(typeof globalThis.Response, 'function'); +{ + const asyncFunction = async function() {}.constructor; + + assert.ok(!(fetch instanceof asyncFunction)); + assert.notStrictEqual(Reflect.getPrototypeOf(fetch), Reflect.getPrototypeOf(async function() {})); + assert.strictEqual(Reflect.getPrototypeOf(fetch), Reflect.getPrototypeOf(function() {})); +} + const server = http.createServer(common.mustCall((req, res) => { res.end('Hello world'); }));