@@ -231,19 +231,18 @@ PromiseWrap* PromiseWrap::New(Environment* env,
231
231
232
232
// Skip for init events
233
233
if (silent) {
234
- Local<Value> maybeAsyncId = promise
234
+ Local<Value> maybe_async_id = promise
235
235
->Get (context, env->async_id_symbol ())
236
236
.ToLocalChecked ();
237
237
238
- Local<Value> maybeTriggerAsyncId = promise
238
+ Local<Value> maybe_trigger_async_id = promise
239
239
->Get (context, env->trigger_async_id_symbol ())
240
240
.ToLocalChecked ();
241
241
242
- if (maybeAsyncId->IsNumber () && maybeTriggerAsyncId->IsNumber ()) {
243
- double asyncId = maybeAsyncId->NumberValue (context).ToChecked ();
244
- double triggerAsyncId = maybeTriggerAsyncId->NumberValue (context)
245
- .ToChecked ();
246
- return new PromiseWrap (env, obj, asyncId, triggerAsyncId);
242
+ if (maybe_async_id->IsNumber () && maybe_trigger_async_id->IsNumber ()) {
243
+ double async_id = maybe_async_id.As <Number>()->Value ();
244
+ double trigger_async_id = maybe_trigger_async_id.As <Number>()->Value ();
245
+ return new PromiseWrap (env, obj, async_id, trigger_async_id);
247
246
}
248
247
}
249
248
@@ -320,6 +319,59 @@ static void FastPromiseHook(PromiseHookType type, Local<Promise> promise,
320
319
Environment* env = Environment::GetCurrent (context);
321
320
if (env == nullptr ) return ;
322
321
322
+ if (type == PromiseHookType::kBefore &&
323
+ env->async_hooks ()->fields ()[AsyncHooks::kBefore ] == 0 ) {
324
+ Local<Value> maybe_async_id;
325
+ if (!promise->Get (context, env->async_id_symbol ())
326
+ .ToLocal (&maybe_async_id)) {
327
+ return ;
328
+ }
329
+
330
+ Local<Value> maybe_trigger_async_id;
331
+ if (!promise->Get (context, env->trigger_async_id_symbol ())
332
+ .ToLocal (&maybe_trigger_async_id)) {
333
+ return ;
334
+ }
335
+
336
+ if (maybe_async_id->IsNumber () && maybe_trigger_async_id->IsNumber ()) {
337
+ double async_id = maybe_async_id.As <Number>()->Value ();
338
+ double trigger_async_id = maybe_trigger_async_id.As <Number>()->Value ();
339
+ env->async_hooks ()->push_async_context (
340
+ async_id, trigger_async_id, promise);
341
+ }
342
+
343
+ return ;
344
+ }
345
+
346
+ if (type == PromiseHookType::kAfter &&
347
+ env->async_hooks ()->fields ()[AsyncHooks::kAfter ] == 0 ) {
348
+ Local<Value> maybe_async_id;
349
+ if (!promise->Get (context, env->async_id_symbol ())
350
+ .ToLocal (&maybe_async_id)) {
351
+ return ;
352
+ }
353
+
354
+ if (maybe_async_id->IsNumber ()) {
355
+ double async_id = maybe_async_id.As <Number>()->Value ();
356
+ if (env->execution_async_id () == async_id) {
357
+ // This condition might not be true if async_hooks was enabled during
358
+ // the promise callback execution.
359
+ env->async_hooks ()->pop_async_context (async_id);
360
+ }
361
+ }
362
+
363
+ return ;
364
+ }
365
+
366
+ if (type == PromiseHookType::kResolve &&
367
+ env->async_hooks ()->fields ()[AsyncHooks::kPromiseResolve ] == 0 ) {
368
+ return ;
369
+ }
370
+
371
+ // Getting up to this point means either init type or
372
+ // that there are active hooks of another type.
373
+ // In both cases fast-path JS hook should be called.
374
+
323
375
Local<Value> argv[] = {
324
376
Integer::New (env->isolate (), ToAsyncHooksType (type)),
325
377
promise,
0 commit comments