/**
* @file promise
*
* promise是一个带有then方法的对象或函数
* value是任何合法的JavaScript值(包括undefined、promiseLike或promise)。
*
* pending态时:
* 可以过渡到完成或拒绝状态。
* fulfilled态时:
* 不能过渡到任何其他状态。
* 必须有一个不能改变的值。
* rejected态时:
* 不能过渡到任何其他状态。
* 必须有一个理由,这个理由不能改变。
*/
export namespace _ {
export type FulfillmentHandler<T, TResult> = ((value: T) => TResult | PromiseLike<TResult>) | undefined | null;
export type RejectionHandler<TResult> = ((reason?: any) => TResult | PromiseLike<TResult>) | undefined | null;
export type FinallyHandler = (() => void) | undefined | null;
export interface PromiseLike<T> {
then<TResult1 = T, TResult2 = never>(
onFulfilled?: FulfillmentHandler<T, TResult1>,
onRejected?: RejectionHandler<TResult2>
): PromiseLike<TResult1 | TResult2>;
}
export type Executor<T> = (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void;
export interface Promise<T> {
then<TResult1 = T, TResult2 = never>(
onFulfilled?: FulfillmentHandler<T, TResult1>,
onRejected?: RejectionHandler<TResult2>
): Promise<TResult1 | TResult2>;
catch<TResult = never>(onRejected?: RejectionHandler<TResult>): Promise<T | TResult>;
finally(onFinally?: FinallyHandler): Promise<T>;
}
export interface PromiseConstructor {
readonly prototype: Promise<any>;
new <T>(
executor: (
resolve: (value: T | PromiseLike<T>) => void,
reject: (reason?: any) => void
) => void
): Promise<T>;
resolve(): Promise<void>;
resolve<T>(value: T | PromiseLike<T>): Promise<T>;
reject<T = never>(reason?: any): Promise<T>;
all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: readonly [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3
Promise ts版实现
最新推荐文章于 2024-05-27 14:49:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)