es6-promise.d.ts 3.2 KB

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