es6-promise.d.ts 3.0 KB

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