perks/tasks/lib/manual-promise.ts

75 строки
2.6 KiB
TypeScript

/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/**
* A manually (or externally) controlled asynchronous Promise implementation
*/
export class ManualPromise<T> implements Promise<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null | undefined): Promise<TResult1 | TResult2> {
return this.p.then(onfulfilled, onrejected);
}
/**
* Attaches a callback for only the rejection of the Promise.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null | undefined): Promise<T | TResult> {
return this.p.catch(onrejected);
}
finally(onfinally?: (() => void) | null | undefined): Promise<T> {
return this.p.finally(onfinally);
}
readonly [Symbol.toStringTag]: 'Promise';
private p: Promise<T>;
/**
* A method to manually resolve the Promise.
*/
public resolve: (value?: T | PromiseLike<T> | undefined) => void = (v) => { };
/**
* A method to manually reject the Promise
*/
public reject: (e: any) => void = (e) => { };
private state: 'pending' | 'resolved' | 'rejected' = 'pending';
/**
* Returns true of the Promise has been Resolved or Rejected
*/
public get isCompleted(): boolean {
return this.state !== 'pending';
}
/**
* Returns true if the Promise has been Resolved.
*/
public get isResolved(): boolean {
return this.state === 'resolved';
}
/**
* Returns true if the Promise has been Rejected.
*/
public get isRejected(): boolean {
return this.state === 'rejected';
}
public constructor() {
this.p = new Promise<T>((r, j) => {
this.resolve = (v: T | PromiseLike<T> | undefined) => { this.state = 'resolved'; r(v as any); };
this.reject = (e: any) => { this.state = 'rejected'; j(e); };
});
}
}