Skip to content

Instantly share code, notes, and snippets.

@wentout
Created April 7, 2025 13:43
Show Gist options
  • Save wentout/f48e6b4376d440a2fdf4d903801672b2 to your computer and use it in GitHub Desktop.
Save wentout/f48e6b4376d440a2fdf4d903801672b2 to your computer and use it in GitHub Desktop.
Native Await New
'use strict';
class MyPromise<T> extends Promise<T> {
constructor(handler: (
resolve: (value: T) => unknown,
reject: () => unknown) => void
) {
super(handler);
}
}
const myItem = new MyPromise((resolve, reject) => {
resolve(123);
});
console.log('myItem instanceof Promise', myItem instanceof Promise);
(async () => {
console.log(await myItem);
console.log(await new MyPromise((resolve) => {
resolve(321);
}));
})();
'use strict';
class MyPromise extends Promise {
#_resolved = false;
#_rejected = false;
#_happyValue
#_unhappyValue
constructor(handler) {
console.log('my promise call 1 : ', new.target);
let resolve, reject;
super((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
const self = this;
handler((happyValue) => {
if (self.#_resolved) {
console.log(' thenable happy value : ', self.#_happyValue, happyValue);
resolve(self.#_happyValue);
return;
}
self.#_happyValue = happyValue;
console.log(' happy value : ', self.#_happyValue);
resolve(self.#_happyValue);
self.#_resolved = true;
}, (unhappyValue) => {
if (self.#_rejected) {
console.log(' thenable unhappy value : ', self.#_unhappyValue, unhappyValue);
reject(self._unhappyValue);
return;
}
self.#_unhappyValue = _unhappyValue;
console.log(' unhappy value : ', self.#_unhappyValue);
reject(self.#_unhappyValue);
self.#_rejected = true;
});
console.log('my promise call 2');
}
get self() {
return this;
}
// get then() {
// const self = this;
// console.log('Then Getter Call');
// return function (cb) {
// console.log('Then Resulter Call');
// if (self.#_thenAlreadyCalled) {
// const answer = cb(self.#_happyValue);
// self.#_happyValue = answer;
// return self;
// }
// self.#_thenAlreadyCalled = true;
// const answer = cb(self.#_happyValue);
// self.#_happyValue = answer;
// return self;
// }
// }
// [Symbol.species]() {
// return Promise;
// }
};
(async () => {
console.log('\n --- ↓↓↓ --- 321 starts here --- ↓↓↓ --- \n')
console.log(' awaited result : ', await new MyPromise((resolve) => {
setTimeout(() => {
const value = 321;
resolve(value);
}, 1000);
}));
console.log('\n --- ↓↓↓ --- chained starts here --- ↓↓↓ --- \n')
console.log(' awaited result : ', await new MyPromise((resolve) => {
setTimeout(() => {
const value = 'chained 1';
resolve(value);
}, 1000);
}).then((value) => {
console.log(value);
return 'chained 2'
}));
console.log('\n --- ↓↓↓ --- 123 starts here --- ↓↓↓ --- \n')
console.time(' spent time ');
const instance = new MyPromise((resolve, reject) => {
console.log('native promise call');
setTimeout(() => {
console.log('pre resolve call');
resolve(123);
console.log('post resolve call');
}, 1000);
});
console.log('\n');
console.log(' instanceof Promise : ', instance instanceof Promise);
console.log(' instanceof MyPromise : ', instance instanceof MyPromise);
console.log(' instance .self : ', instance.self);
console.log('\n');
instance
.then((result) => {
console.log('\n');
console.log(' then result : ', result);
console.log(' then this : ', Object.getPrototypeOf(this));
console.log(' instance .self : ', instance.self);
console.log('\n');
console.timeEnd(' spent time ');
console.log('\n');
return 345;
})
.then((result) => {
console.log('see additional .constructor invocation ', result);
console.log('\n');
return 567;
})
.then((result) => {
console.log('see additional .constructor invocation ', result);
console.log('\n');
return 789;
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment