promises: Add a comment explaining a subtlety in `Gate`

pull/5347/head
Richard Hansen 2022-01-02 18:56:33 -05:00
parent a115c475ad
commit fa8bdb0348
1 changed files with 3 additions and 0 deletions

View File

@ -65,6 +65,9 @@ class Gate extends Promise {
static get [Symbol.species]() { return Promise; } static get [Symbol.species]() { return Promise; }
constructor() { constructor() {
// `this` is assigned when `super()` returns, not when it is called, so it is not acceptable to
// do the following because it will throw a ReferenceError when it dereferences `this`:
// super((resolve, reject) => Object.assign(this, {resolve, reject}));
let props; let props;
super((resolve, reject) => props = {resolve, reject}); super((resolve, reject) => props = {resolve, reject});
Object.assign(this, props); Object.assign(this, props);