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