Skip to content

Commit

Permalink
WIP: change thenable detection
Browse files Browse the repository at this point in the history
  • Loading branch information
JakobJingleheimer committed Jun 12, 2022
1 parent 8bf7965 commit 8425665
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions lib/internal/modules/esm/loader.js
Expand Up @@ -15,6 +15,8 @@ const {
ObjectDefineProperty,
ObjectSetPrototypeOf,
PromiseAll,
PromiseResolve,
PromisePrototypeThen,
ReflectApply,
RegExpPrototypeExec,
SafeArrayIterator,
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 8425665

Please sign in to comment.