Skip to content

Instantly share code, notes, and snippets.

@MarekZeman91
Last active February 6, 2023 11:40
Show Gist options
  • Save MarekZeman91/324ecf4764a79b8b02ac831104680c7d to your computer and use it in GitHub Desktop.
Save MarekZeman91/324ecf4764a79b8b02ac831104680c7d to your computer and use it in GitHub Desktop.
class ExpandedPromise<T> extends Promise<T> {
resolve: (value: T | PromiseLike<T>) => void;
reject: (reason?: any) => void;
status: 'pending' | 'fulfilled' | 'rejected' = 'pending';
constructor() {
const callbacks: {
resolve?: ExpandedPromise<T>['resolve'],
reject?: ExpandedPromise<T>['reject'],
} = {};
super((resolve, reject) => {
callbacks.resolve = (...args) => {
if (this.status === 'pending') {
this.status = 'fulfilled';
}
resolve(...args);
};
callbacks.reject = (...args) => {
if (this.status === 'pending') {
this.status = 'rejected';
}
reject(...args);
};
});
Object.assign(this, callbacks);
}
static get [Symbol.species]() {
return Promise;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment