yaku.d.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. export interface Thenable<R> {
  6. then<U>(onFulfilled?: (value: R) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U> | void): Thenable<U>;
  7. }
  8. export default 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 reject.
  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> | void): 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. * Make a new promise from the thenable.
  36. * A thenable is promise-like in as far as it has a "then" method.
  37. */
  38. static resolve<R>(value?: R | Thenable<R>): Promise<R>;
  39. /**
  40. * Make a promise that rejects to obj. For consistency and debugging (eg stack traces), obj should be an instanceof Error
  41. */
  42. static reject(error: any): Promise<any>;
  43. /**
  44. * Make a promise that fulfills when every item in the array fulfills, and rejects if (and when) any item rejects.
  45. * the array passed to all can be a mixture of promise-like objects and other objects.
  46. * The fulfillment value is an array (in order) of fulfillment values. The rejection value is the first rejection value.
  47. */
  48. static all<R>(promises: (R | Thenable<R>)[]): Promise<R[]>;
  49. /**
  50. * Make a Promise that fulfills when any item fulfills, and rejects if any item rejects.
  51. */
  52. static race<R>(promises: (R | Thenable<R>)[]): Promise<R>;
  53. }