|
| 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 | + |
| 6 | +interface 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 | +} |
| 10 | + |
| 11 | +declare class Promise<R> implements Thenable<R> { |
| 12 | + /** |
| 13 | + * If you call resolve in the body of the callback passed to the constructor, |
| 14 | + * your promise is fulfilled with result object passed to resolve. |
| 15 | + * If you call reject your promise is rejected with the object passed to reject. |
| 16 | + * For consistency and debugging (eg stack traces), obj should be an instanceof Error. |
| 17 | + * Any errors thrown in the constructor callback will be implicitly passed to reject(). |
| 18 | + */ |
| 19 | + constructor(callback: (resolve : (value?: R | Thenable<R>) => void, reject: (error?: any) => void) => void); |
| 20 | + |
| 21 | + /** |
| 22 | + * onFulfilled is called when/if "promise" resolves. onRejected is called when/if "promise" rejects. |
| 23 | + * Both are optional, if either/both are omitted the next onFulfilled/onRejected in the chain is called. |
| 24 | + * Both callbacks have a single parameter , the fulfillment value or rejection reason. |
| 25 | + * "then" returns a new promise equivalent to the value you return from onFulfilled/onRejected after being passed through Promise.resolve. |
| 26 | + * If an error is thrown in the callback, the returned promise rejects with that error. |
| 27 | + * |
| 28 | + * @param onFulfilled called when/if "promise" resolves |
| 29 | + * @param onRejected called when/if "promise" rejects |
| 30 | + */ |
| 31 | + then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): Promise<U>; |
| 32 | + then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => void): Promise<U>; |
| 33 | + |
| 34 | + /** |
| 35 | + * Sugar for promise.then(undefined, onRejected) |
| 36 | + * |
| 37 | + * @param onRejected called when/if "promise" rejects |
| 38 | + */ |
| 39 | + catch<U>(onRejected?: (error: any) => U | Thenable<U>): Promise<U>; |
| 40 | +} |
| 41 | + |
| 42 | +declare module Promise { |
| 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 | + function resolve<R>(value?: R | Thenable<R>): Promise<R>; |
| 48 | + |
| 49 | + /** |
| 50 | + * Make a promise that rejects to obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error |
| 51 | + */ |
| 52 | + function reject(error: any): Promise<any>; |
| 53 | + |
| 54 | + /** |
| 55 | + * Make a promise that fulfills when every item in the array fulfills, and rejects if (and when) any item rejects. |
| 56 | + * the array passed to all can be a mixture of promise-like objects and other objects. |
| 57 | + * The fulfillment value is an array (in order) of fulfillment values. The rejection value is the first rejection value. |
| 58 | + */ |
| 59 | + function all<R>(promises: (R | Thenable<R>)[]): Promise<R[]>; |
| 60 | + |
| 61 | + /** |
| 62 | + * Make a Promise that fulfills when any item fulfills, and rejects if any item rejects. |
| 63 | + */ |
| 64 | + function race<R>(promises: (R | Thenable<R>)[]): Promise<R>; |
| 65 | +} |
| 66 | + |
| 67 | +declare module 'es6-promise' { |
| 68 | + var foo: typeof Promise; // Temp variable to reference Promise in local context |
| 69 | + module rsvp { |
| 70 | + export var Promise: typeof foo; |
| 71 | + } |
| 72 | + export = rsvp; |
| 73 | +} |
0 commit comments