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