From 84256658bdadb2a24aefee30b9ac0a665aaf4a0e Mon Sep 17 00:00:00 2001 From: Jacob Smith <3012099+JakobJingleheimer@users.noreply.github.com> Date: Sun, 12 Jun 2022 15:50:25 +0200 Subject: [PATCH] WIP: change thenable detection --- lib/internal/modules/esm/loader.js | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index 6897c0e9bbfae2..284c51f498ae2f 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -15,6 +15,8 @@ const { ObjectDefineProperty, ObjectSetPrototypeOf, PromiseAll, + PromiseResolve, + PromisePrototypeThen, ReflectApply, RegExpPrototypeExec, SafeArrayIterator, @@ -158,28 +160,15 @@ function nextHookFactory(chain, meta, validate) { if (generatedHookIndex === 0) { meta.chainFinished = true; } ArrayPrototypePush(args, nextNextHook); - const output = ReflectApply(hook, undefined, args); + let output = ReflectApply(hook, undefined, args); function checkShortCircuited(output) { if (output?.shortCircuit === true) { meta.shortCircuited = true; } } - const then = output?.then; - if (typeof then === 'function') { - if (!meta.isChainAsync) { - throw ERR_INVALID_RETURN_VALUE( - 'an object', - // MUST use generatedHookIndex because the chain has already advanced, - // causing meta.hookIndex to advance - `${chain[generatedHookIndex].url} '${hookName}' hook's ${nextHookName}()`, - output, - ); - } - - ReflectApply(then, output, [ - checkShortCircuited, - // TODO: handle error case - ]); + if (meta.isChainAsync) { + output = PromiseResolve(output); + PromisePrototypeThen(output, checkShortCircuited); } else { checkShortCircuited(output); }