Created
May 23, 2025 18:22
-
-
Save jordangarcia/e9ca64c43ae14ee5d29c02f9996cd677 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CustomPromise<R, R2 = unknown> { | |
public status: 'awaiting' | 'resolved' | 'rejected' = 'awaiting'; | |
public resolvedValue: R | undefined = undefined; | |
public rejectedReason: R2 | undefined = undefined; | |
private successFns: ((val: R) => void)[] = []; | |
private rejectFns: ((reason: R2) => void)[] = []; | |
static resolve<SR>(val: SR): CustomPromise<SR> { | |
const re = new CustomPromise<SR>(() => {}); | |
re.status = 'resolved'; | |
re.resolvedValue = val; | |
return re; | |
} | |
static reject<SR>(rejectedReason: SR): CustomPromise<never, SR> { | |
const re = new CustomPromise<never, SR>(() => {}); | |
re.status = 'rejected'; | |
re.rejectedReason = rejectedReason; | |
return re; | |
} | |
constructor(executorFn: (resolve: (val: R) => void, reject: (reason: R2) => void) => void) { | |
setTimeout(() => { | |
executorFn(this.resolve.bind(this), this.reject.bind(this)); | |
}, 0); | |
} | |
private resolve(val: R) { | |
if (this.status === 'awaiting') { | |
this.status = 'resolved'; | |
this.resolvedValue = val; | |
this.successFns.forEach(fn => fn(val)); | |
} | |
} | |
private reject(reason: R2) { | |
if (this.status === 'awaiting') { | |
this.rejectFns.forEach(fn => fn(reason)); | |
this.rejectedReason = reason; | |
this.status = 'rejected'; | |
} | |
// handle thennables | |
} | |
then<NR, NR2>(resolveFn: (val: R) => NR, rejectFn?: (reason: R2) => NR2): CustomPromise<NR, NR2> { | |
return new CustomPromise<NR, NR2>((resolve, reject) => { | |
if (this.status === 'resolved') { | |
resolve(resolveFn(this.resolvedValue!)); | |
return; | |
} | |
if (this.status === 'rejected' && rejectFn) { | |
reject(rejectFn(this.rejectedReason!)); | |
return; | |
} | |
this.successFns.push(resolveValue => { | |
const results = resolveFn(resolveValue); | |
// if this is a promise await it | |
if (results && typeof results === 'object' && 'then' in results) { | |
const resultIsPromise = results as any as CustomPromise<any, any>; | |
resultIsPromise.then(resolve, reject); | |
return; | |
} else { | |
resolve(results); | |
} | |
}); | |
if (rejectFn) { | |
this.rejectFns.push(rejectReason => { | |
const results = rejectFn(rejectReason); | |
if (results && typeof results === 'object' && 'then' in results) { | |
const resultIsPromise = results as any as CustomPromise<any, any>; | |
resultIsPromise.then(resolve, reject); | |
return; | |
} else { | |
reject(results); | |
} | |
}); | |
} | |
}); | |
} | |
} | |
async function main() { | |
const promise = new CustomPromise<string>((resolve, reject) => { | |
setTimeout(() => { | |
resolve('hello'); | |
}, 100); | |
}); | |
const waitwaitwait = promise.then(val => { | |
console.log('then value', val); | |
return 'transformed'; | |
}); | |
const a = await waitwaitwait; | |
console.log(a); | |
promise.then(v => { | |
console.log('already resolved', v); | |
}); | |
} | |
await main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment