Skip to content

Commit 9da102f

Browse files
ofrobotsMylesBorins
authored andcommitted
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 f1ff107 commit 9da102f

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
*Note*: In some cases the resource object is reused for performance reasons,
310310
it is 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
@@ -239,7 +239,7 @@ class PromiseWrap : public AsyncWrap {
239239
size_t self_size() const override { return sizeof(*this); }
240240

241241
static constexpr int kPromiseField = 1;
242-
static constexpr int kParentAsyncIdField = 2;
242+
static constexpr int kIsChainedPromiseField = 2;
243243
static constexpr int kInternalFieldCount = 3;
244244

245245
static PromiseWrap* New(Environment* env,
@@ -248,8 +248,8 @@ class PromiseWrap : public AsyncWrap {
248248
bool silent);
249249
static void GetPromise(Local<String> property,
250250
const PropertyCallbackInfo<Value>& info);
251-
static void getParentAsyncId(Local<String> property,
252-
const PropertyCallbackInfo<Value>& info);
251+
static void getIsChainedPromise(Local<String> property,
252+
const PropertyCallbackInfo<Value>& info);
253253
};
254254

255255
PromiseWrap* PromiseWrap::New(Environment* env,
@@ -259,11 +259,10 @@ PromiseWrap* PromiseWrap::New(Environment* env,
259259
Local<Object> object = env->promise_wrap_template()
260260
->NewInstance(env->context()).ToLocalChecked();
261261
object->SetInternalField(PromiseWrap::kPromiseField, promise);
262-
if (parent_wrap != nullptr) {
263-
object->SetInternalField(PromiseWrap::kParentAsyncIdField,
264-
Number::New(env->isolate(),
265-
parent_wrap->get_async_id()));
266-
}
262+
object->SetInternalField(PromiseWrap::kIsChainedPromiseField,
263+
parent_wrap != nullptr ?
264+
v8::True(env->isolate()) :
265+
v8::False(env->isolate()));
267266
CHECK_EQ(promise->GetAlignedPointerFromInternalField(0), nullptr);
268267
promise->SetInternalField(0, object);
269268
return new PromiseWrap(env, object, silent);
@@ -274,10 +273,10 @@ void PromiseWrap::GetPromise(Local<String> property,
274273
info.GetReturnValue().Set(info.Holder()->GetInternalField(kPromiseField));
275274
}
276275

277-
void PromiseWrap::getParentAsyncId(Local<String> property,
278-
const PropertyCallbackInfo<Value>& info) {
276+
void PromiseWrap::getIsChainedPromise(Local<String> property,
277+
const PropertyCallbackInfo<Value>& info) {
279278
info.GetReturnValue().Set(
280-
info.Holder()->GetInternalField(kParentAsyncIdField));
279+
info.Holder()->GetInternalField(kIsChainedPromiseField));
281280
}
282281

283282
static void PromiseHook(PromiseHookType type, Local<Promise> promise,
@@ -377,8 +376,8 @@ static void SetupHooks(const FunctionCallbackInfo<Value>& args) {
377376
FIXED_ONE_BYTE_STRING(env->isolate(), "promise"),
378377
PromiseWrap::GetPromise);
379378
promise_wrap_template->SetAccessor(
380-
FIXED_ONE_BYTE_STRING(env->isolate(), "parentId"),
381-
PromiseWrap::getParentAsyncId);
379+
FIXED_ONE_BYTE_STRING(env->isolate(), "isChainedPromise"),
380+
PromiseWrap::getIsChainedPromise);
382381
env->set_promise_wrap_template(promise_wrap_template);
383382
}
384383
}

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)