index.d.ts 5.2 KB

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