@@ -162,19 +162,25 @@ static void DestroyAsyncIdsCallback(void* arg) {
162162}
163163
164164
165- void AsyncWrap::EmitPromiseResolve (Environment* env, double async_id) {
165+ void Emit (Environment* env, double async_id, AsyncHooks::Fields type,
166+ Local<Function> fn) {
166167 AsyncHooks* async_hooks = env->async_hooks ();
167168
168- if (async_hooks->fields ()[AsyncHooks:: kPromiseResolve ] == 0 )
169+ if (async_hooks->fields ()[type ] == 0 )
169170 return ;
170171
171172 Local<Value> async_id_value = Number::New (env->isolate (), async_id);
172- Local<Function> fn = env->async_hooks_promise_resolve_function ();
173173 FatalTryCatch try_catch (env);
174174 USE (fn->Call (env->context (), Undefined (env->isolate ()), 1 , &async_id_value));
175175}
176176
177177
178+ void AsyncWrap::EmitPromiseResolve (Environment* env, double async_id) {
179+ Emit (env, async_id, AsyncHooks::kPromiseResolve ,
180+ env->async_hooks_promise_resolve_function ());
181+ }
182+
183+
178184void AsyncWrap::EmitTraceEventBefore () {
179185 switch (provider_type ()) {
180186#define V (PROVIDER ) \
@@ -192,15 +198,8 @@ void AsyncWrap::EmitTraceEventBefore() {
192198
193199
194200void AsyncWrap::EmitBefore (Environment* env, double async_id) {
195- AsyncHooks* async_hooks = env->async_hooks ();
196-
197- if (async_hooks->fields ()[AsyncHooks::kBefore ] == 0 )
198- return ;
199-
200- Local<Value> async_id_value = Number::New (env->isolate (), async_id);
201- Local<Function> fn = env->async_hooks_before_function ();
202- FatalTryCatch try_catch (env);
203- USE (fn->Call (env->context (), Undefined (env->isolate ()), 1 , &async_id_value));
201+ Emit (env, async_id, AsyncHooks::kBefore ,
202+ env->async_hooks_before_function ());
204203}
205204
206205
@@ -221,17 +220,10 @@ void AsyncWrap::EmitTraceEventAfter() {
221220
222221
223222void AsyncWrap::EmitAfter (Environment* env, double async_id) {
224- AsyncHooks* async_hooks = env->async_hooks ();
225-
226- if (async_hooks->fields ()[AsyncHooks::kAfter ] == 0 )
227- return ;
228-
229223 // If the user's callback failed then the after() hooks will be called at the
230224 // end of _fatalException().
231- Local<Value> async_id_value = Number::New (env->isolate (), async_id);
232- Local<Function> fn = env->async_hooks_after_function ();
233- FatalTryCatch try_catch (env);
234- USE (fn->Call (env->context (), Undefined (env->isolate ()), 1 , &async_id_value));
225+ Emit (env, async_id, AsyncHooks::kAfter ,
226+ env->async_hooks_after_function ());
235227}
236228
237229class PromiseWrap : public AsyncWrap {
0 commit comments