UNPKG

3.37 kBTypeScriptView Raw
1// Type definitions for es6-promise
2// Project: https://github.com/jakearchibald/ES6-Promise
3// Definitions by: François de Campredon <https://github.com/fdecampredon/>, vvakame <https://github.com/vvakame>
4// Definitions: https://github.com/borisyankov/DefinitelyTyped
5
6interface Thenable<R> {
7 then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
8 then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => void): Thenable<U>;
9 catch<U>(onRejected?: (error: any) => U | Thenable<U>): Thenable<U>;
10}
11
12declare class Promise<R> implements Thenable<R> {
13 /**
14 * If you call resolve in the body of the callback passed to the constructor,
15 * your promise is fulfilled with result object passed to resolve.
16 * If you call reject your promise is rejected with the object passed to reject.
17 * For consistency and debugging (eg stack traces), obj should be an instanceof Error.
18 * Any errors thrown in the constructor callback will be implicitly passed to reject().
19 */
20 constructor(callback: (resolve : (value?: R | Thenable<R>) => void, reject: (error?: any) => void) => void);
21
22 /**
23 * onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects.
24 * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called.
25 * Both callbacks have a single parameter , the fulfillment value or rejection reason.
26 * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve.
27 * If an error is thrown in the callback, the returned promise rejects with that error.
28 *
29 * @param onFulfilled called when/if "promise" resolves
30 * @param onRejected called when/if "promise" rejects
31 */
32 then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Promise<U>;
33 then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => void): Promise<U>;
34
35 /**
36 * Sugar for promise.then(undefined, onRejected)
37 *
38 * @param onRejected called when/if "promise" rejects
39 */
40 catch<U>(onRejected?: (error: any) => U | Thenable<U>): Promise<U>;
41}
42
43declare module Promise {
44 /**
45 * Make a new promise from the thenable.
46 * A thenable is promise-like in as far as it has a "then" method.
47 */
48 function 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 function reject(error: any): Promise<any>;
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 function all<R>(promises: (R | Thenable<R>)[]): Promise<R[]>;
61
62 /**
63 * Make a Promise that fulfills when any item fulfills, and rejects if any item rejects.
64 */
65 function race<R>(promises: (R | Thenable<R>)[]): Promise<R>;
66}
67
68declare module 'es6-promise' {
69 var foo: typeof Promise; // Temp variable to reference Promise in local context
70 module rsvp {
71 export var Promise: typeof foo;
72 }
73 export = rsvp;
74}
75
\No newline at end of file