@@ -162,19 +162,25 @@ static void DestroyAsyncIdsCallback(void* arg) {
162
162
}
163
163
164
164
165
- void AsyncWrap::EmitPromiseResolve (Environment* env, double async_id) {
165
+ void Emit (Environment* env, double async_id, AsyncHooks::Fields type,
166
+ Local<Function> fn) {
166
167
AsyncHooks* async_hooks = env->async_hooks ();
167
168
168
- if (async_hooks->fields ()[AsyncHooks:: kPromiseResolve ] == 0 )
169
+ if (async_hooks->fields ()[type ] == 0 )
169
170
return ;
170
171
171
172
Local<Value> async_id_value = Number::New (env->isolate (), async_id);
172
- Local<Function> fn = env->async_hooks_promise_resolve_function ();
173
173
FatalTryCatch try_catch (env);
174
174
USE (fn->Call (env->context (), Undefined (env->isolate ()), 1 , &async_id_value));
175
175
}
176
176
177
177
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
+
178
184
void AsyncWrap::EmitTraceEventBefore () {
179
185
switch (provider_type ()) {
180
186
#define V (PROVIDER ) \
@@ -192,15 +198,8 @@ void AsyncWrap::EmitTraceEventBefore() {
192
198
193
199
194
200
void 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 ());
204
203
}
205
204
206
205
@@ -221,17 +220,10 @@ void AsyncWrap::EmitTraceEventAfter() {
221
220
222
221
223
222
void 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
-
229
223
// If the user's callback failed then the after() hooks will be called at the
230
224
// 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 ());
235
227
}
236
228
237
229
class PromiseWrap : public AsyncWrap {
0 commit comments