Skip to content

Commit 8425665

Browse files
WIP: change thenable detection
1 parent 8bf7965 commit 8425665

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

lib/internal/modules/esm/loader.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const {
1515
ObjectDefineProperty,
1616
ObjectSetPrototypeOf,
1717
PromiseAll,
18+
PromiseResolve,
19+
PromisePrototypeThen,
1820
ReflectApply,
1921
RegExpPrototypeExec,
2022
SafeArrayIterator,
@@ -158,28 +160,15 @@ function nextHookFactory(chain, meta, validate) {
158160
if (generatedHookIndex === 0) { meta.chainFinished = true; }
159161

160162
ArrayPrototypePush(args, nextNextHook);
161-
const output = ReflectApply(hook, undefined, args);
163+
let output = ReflectApply(hook, undefined, args);
162164

163165
function checkShortCircuited(output) {
164166
if (output?.shortCircuit === true) { meta.shortCircuited = true; }
165167
}
166168

167-
const then = output?.then;
168-
if (typeof then === 'function') {
169-
if (!meta.isChainAsync) {
170-
throw ERR_INVALID_RETURN_VALUE(
171-
'an object',
172-
// MUST use generatedHookIndex because the chain has already advanced,
173-
// causing meta.hookIndex to advance
174-
`${chain[generatedHookIndex].url} '${hookName}' hook's ${nextHookName}()`,
175-
output,
176-
);
177-
}
178-
179-
ReflectApply(then, output, [
180-
checkShortCircuited,
181-
// TODO: handle error case
182-
]);
169+
if (meta.isChainAsync) {
170+
output = PromiseResolve(output);
171+
PromisePrototypeThen(output, checkShortCircuited);
183172
} else {
184173
checkShortCircuited(output);
185174
}

0 commit comments

Comments
 (0)