Skip to content

Commit 9f6a565

Browse files
committed
async_hooks: rename PromiseWrap.parentId
Rename the `parentId` property on the PromiseWrap object to a `isChainedPromise` property. The former wasn't quite useful as it was always defined to be the same value as the trigger id available in the init hook. Instead rename the property to be closer to the information it communicates: whether the promise is a chained promise or not. PR-URL: #18633 Fixes: #18470 Reviewed-By: Andreas Madsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
1 parent bd4773a commit 9f6a565

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

doc/api/async_hooks.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,10 @@ and document their own resource objects. For example, such a resource object
301301
could contain the SQL query being executed.
302302

303303
In the case of Promises, the `resource` object will have `promise` property
304-
that refers to the Promise that is being initialized, and a `parentId` property
305-
set to the `asyncId` of a parent Promise, if there is one, and `undefined`
304+
that refers to the Promise that is being initialized, and a `isChainedPromise`
305+
property, set to `true` if the promise has a parent promise, and `false`
306306
otherwise. For example, in the case of `b = a.then(handler)`, `a` is considered
307-
a parent Promise of `b`.
307+
a parent Promise of `b`. Here, `b` is considered a chained promise.
308308

309309
In some cases the resource object is reused for performance reasons, it is
310310
thus not safe to use it as a key in a `WeakMap` or add properties to it.

src/async_wrap.cc

+12-13
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ class PromiseWrap : public AsyncWrap {
245245
size_t self_size() const override { return sizeof(*this); }
246246

247247
static constexpr int kPromiseField = 1;
248-
static constexpr int kParentAsyncIdField = 2;
248+
static constexpr int kIsChainedPromiseField = 2;
249249
static constexpr int kInternalFieldCount = 3;
250250

251251
static PromiseWrap* New(Environment* env,
@@ -254,8 +254,8 @@ class PromiseWrap : public AsyncWrap {
254254
bool silent);
255255
static void GetPromise(Local<String> property,
256256
const PropertyCallbackInfo<Value>& info);
257-
static void getParentAsyncId(Local<String> property,
258-
const PropertyCallbackInfo<Value>& info);
257+
static void getIsChainedPromise(Local<String> property,
258+
const PropertyCallbackInfo<Value>& info);
259259
};
260260

261261
PromiseWrap* PromiseWrap::New(Environment* env,
@@ -265,11 +265,10 @@ PromiseWrap* PromiseWrap::New(Environment* env,
265265
Local<Object> object = env->promise_wrap_template()
266266
->NewInstance(env->context()).ToLocalChecked();
267267
object->SetInternalField(PromiseWrap::kPromiseField, promise);
268-
if (parent_wrap != nullptr) {
269-
object->SetInternalField(PromiseWrap::kParentAsyncIdField,
270-
Number::New(env->isolate(),
271-
parent_wrap->get_async_id()));
272-
}
268+
object->SetInternalField(PromiseWrap::kIsChainedPromiseField,
269+
parent_wrap != nullptr ?
270+
v8::True(env->isolate()) :
271+
v8::False(env->isolate()));
273272
CHECK_EQ(promise->GetAlignedPointerFromInternalField(0), nullptr);
274273
promise->SetInternalField(0, object);
275274
return new PromiseWrap(env, object, silent);
@@ -280,10 +279,10 @@ void PromiseWrap::GetPromise(Local<String> property,
280279
info.GetReturnValue().Set(info.Holder()->GetInternalField(kPromiseField));
281280
}
282281

283-
void PromiseWrap::getParentAsyncId(Local<String> property,
284-
const PropertyCallbackInfo<Value>& info) {
282+
void PromiseWrap::getIsChainedPromise(Local<String> property,
283+
const PropertyCallbackInfo<Value>& info) {
285284
info.GetReturnValue().Set(
286-
info.Holder()->GetInternalField(kParentAsyncIdField));
285+
info.Holder()->GetInternalField(kIsChainedPromiseField));
287286
}
288287

289288
static void PromiseHook(PromiseHookType type, Local<Promise> promise,
@@ -383,8 +382,8 @@ static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
383382
FIXED_ONE_BYTE_STRING(env->isolate(), "promise"),
384383
PromiseWrap::GetPromise);
385384
promise_wrap_template->SetAccessor(
386-
FIXED_ONE_BYTE_STRING(env->isolate(), "parentId"),
387-
PromiseWrap::getParentAsyncId);
385+
FIXED_ONE_BYTE_STRING(env->isolate(), "isChainedPromise"),
386+
PromiseWrap::getIsChainedPromise);
388387
env->set_promise_wrap_template(promise_wrap_template);
389388
}
390389
}

test/parallel/test-async-hooks-promise.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const a = Promise.resolve(42);
2121
const b = a.then(common.mustCall());
2222

2323
assert.strictEqual(initCalls[0].triggerId, 1);
24-
assert.strictEqual(initCalls[0].resource.parentId, undefined);
24+
assert.strictEqual(initCalls[0].resource.isChainedPromise, false);
2525
assert.strictEqual(initCalls[0].resource.promise, a);
2626
assert.strictEqual(initCalls[1].triggerId, initCalls[0].id);
27-
assert.strictEqual(initCalls[1].resource.parentId, initCalls[0].id);
27+
assert.strictEqual(initCalls[1].resource.isChainedPromise, true);
2828
assert.strictEqual(initCalls[1].resource.promise, b);

0 commit comments

Comments
 (0)