@@ -15,6 +15,8 @@ const {
15
15
ObjectDefineProperty,
16
16
ObjectSetPrototypeOf,
17
17
PromiseAll,
18
+ PromiseResolve,
19
+ PromisePrototypeThen,
18
20
ReflectApply,
19
21
RegExpPrototypeExec,
20
22
SafeArrayIterator,
@@ -158,28 +160,15 @@ function nextHookFactory(chain, meta, validate) {
158
160
if ( generatedHookIndex === 0 ) { meta . chainFinished = true ; }
159
161
160
162
ArrayPrototypePush ( args , nextNextHook ) ;
161
- const output = ReflectApply ( hook , undefined , args ) ;
163
+ let output = ReflectApply ( hook , undefined , args ) ;
162
164
163
165
function checkShortCircuited ( output ) {
164
166
if ( output ?. shortCircuit === true ) { meta . shortCircuited = true ; }
165
167
}
166
168
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 ) ;
183
172
} else {
184
173
checkShortCircuited ( output ) ;
185
174
}
0 commit comments