| 1 | export interface Thenable <R> { |
| 2 | then <U> (onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>; |
| 3 | then <U> (onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>; |
| 4 | } |
| 5 | |
| 6 | export class Promise <R> implements Thenable <R> { |
| 7 | /** |
| 8 | * If you call resolve in the body of the callback passed to the constructor, |
| 9 | * your promise is fulfilled with result object passed to resolve. |
| 10 | * If you call reject your promise is rejected with the object passed to resolve. |
| 11 | * For consistency and debugging (eg stack traces), obj should be an instanceof Error. |
| 12 | * Any errors thrown in the constructor callback will be implicitly passed to reject(). |
| 13 | */ |
| 14 | constructor (callback: (resolve : (value?: R | Thenable<R>) => void, reject: (error?: any) => void) => void); |
| 15 | |
| 16 | /** |
| 17 | * onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. |
| 18 | * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. |
| 19 | * Both callbacks have a single parameter , the fulfillment value or rejection reason. |
| 20 | * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. |
| 21 | * If an error is thrown in the callback, the returned promise rejects with that error. |
| 22 | * |
| 23 | * @param onFulfilled called when/if "promise" resolves |
| 24 | * @param onRejected called when/if "promise" rejects |
| 25 | */ |
| 26 | then <U> (onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Promise<U>; |
| 27 | then <U> (onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => void): Promise<U>; |
| 28 | |
| 29 | /** |
| 30 | * Sugar for promise.then(undefined, onRejected) |
| 31 | * |
| 32 | * @param onRejected called when/if "promise" rejects |
| 33 | */ |
| 34 | catch <U> (onRejected?: (error: any) => U | Thenable<U>): Promise<U>; |
| 35 | |
| 36 | /** |
| 37 | * onSettled is invoked when/if the "promise" settles (either rejects or fulfills); |
| 38 | * |
| 39 | * @param onFinally called when/if "promise" settles |
| 40 | */ |
| 41 | finally <U> (onFinally?: (callback: any) => U | Thenable<U>): Promise<U>; |
| 42 | |
| 43 | /** |
| 44 | * Make a new promise from the thenable. |
| 45 | * A thenable is promise-like in as far as it has a "then" method. |
| 46 | */ |
| 47 | static resolve (): Promise<void>; |
| 48 | static resolve <R> (value: R | Thenable<R>): Promise<R>; |
| 49 | |
| 50 | /** |
| 51 | * Make a promise that rejects to obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error |
| 52 | */ |
| 53 | static reject <R> (error: any): Promise<R>; |
| 54 | |
| 55 | /** |
| 56 | * Make a promise that fulfills when every item in the array fulfills, and rejects if (and when) any item rejects. |
| 57 | * the array passed to all can be a mixture of promise-like objects and other objects. |
| 58 | * The fulfillment value is an array (in order) of fulfillment values. The rejection value is the first rejection value. |
| 59 | */ |
| 60 | static all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>, T10 | Thenable<T10>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; |
| 61 | static all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; |
| 62 | static all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>]): Promise<[T1, T2, T3, T4, T5, T6, T7, T8]>; |
| 63 | static all<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>]): Promise<[T1, T2, T3, T4, T5, T6, T7]>; |
| 64 | static all<T1, T2, T3, T4, T5, T6>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>]): Promise<[T1, T2, T3, T4, T5, T6]>; |
| 65 | static all<T1, T2, T3, T4, T5>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>]): Promise<[T1, T2, T3, T4, T5]>; |
| 66 | static all<T1, T2, T3, T4>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>]): Promise<[T1, T2, T3, T4]>; |
| 67 | static all<T1, T2, T3>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>]): Promise<[T1, T2, T3]>; |
| 68 | static all<T1, T2>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>]): Promise<[T1, T2]>; |
| 69 | static all<T1>(values: [T1 | Thenable<T1>]): Promise<[T1]>; |
| 70 | static all<TAll>(values: Array<TAll | Thenable<TAll>>): Promise<TAll[]>; |
| 71 | |
| 72 | /** |
| 73 | * Make a Promise that fulfills when any item fulfills, and rejects if any item rejects. |
| 74 | */ |
| 75 | static race <R> (promises: (R | Thenable<R>)[]): Promise<R>; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * The polyfill method will patch the global environment (in this case to the Promise name) when called. |
| 80 | */ |
| 81 | export function polyfill (): void; |