index.d.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. interface Thenable<T> {
  2. /**
  3. * Attaches callbacks for the resolution and/or rejection of the ThenPromise.
  4. * @param onfulfilled The callback to execute when the ThenPromise is resolved.
  5. * @param onrejected The callback to execute when the ThenPromise is rejected.
  6. * @returns A ThenPromise for the completion of which ever callback is executed.
  7. */
  8. then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | Thenable<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | Thenable<TResult2>) | undefined | null): Thenable<TResult1 | TResult2>;
  9. }
  10. /**
  11. * Represents the completion of an asynchronous operation
  12. */
  13. interface ThenPromise<T> {
  14. /**
  15. * Attaches callbacks for the resolution and/or rejection of the ThenPromise.
  16. * @param onfulfilled The callback to execute when the ThenPromise is resolved.
  17. * @param onrejected The callback to execute when the ThenPromise is rejected.
  18. * @returns A ThenPromise for the completion of which ever callback is executed.
  19. */
  20. then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | Thenable<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | Thenable<TResult2>) | undefined | null): ThenPromise<TResult1 | TResult2>;
  21. /**
  22. * Attaches a callback for only the rejection of the ThenPromise.
  23. * @param onrejected The callback to execute when the ThenPromise is rejected.
  24. * @returns A ThenPromise for the completion of the callback.
  25. */
  26. catch<TResult = never>(onrejected?: ((reason: any) => TResult | Thenable<TResult>) | undefined | null): ThenPromise<T | TResult>;
  27. // Extensions specific to then/promise
  28. /**
  29. * Attaches callbacks for the resolution and/or rejection of the ThenPromise, without returning a new promise.
  30. * @param onfulfilled The callback to execute when the ThenPromise is resolved.
  31. * @param onrejected The callback to execute when the ThenPromise is rejected.
  32. */
  33. done(onfulfilled?: ((value: T) => any) | undefined | null, onrejected?: ((reason: any) => any) | undefined | null): void;
  34. /**
  35. * Calls a node.js style callback. If none is provided, the promise is returned.
  36. */
  37. nodeify(callback: void | null): ThenPromise<T>;
  38. nodeify(callback: (err: Error, value: T) => void): void;
  39. }
  40. interface ThenPromiseConstructor {
  41. /**
  42. * A reference to the prototype.
  43. */
  44. readonly prototype: ThenPromise<any>;
  45. /**
  46. * Creates a new ThenPromise.
  47. * @param executor A callback used to initialize the promise. This callback is passed two arguments:
  48. * a resolve callback used resolve the promise with a value or the result of another promise,
  49. * and a reject callback used to reject the promise with a provided reason or error.
  50. */
  51. new <T>(executor: (resolve: (value?: T | Thenable<T>) => void, reject: (reason?: any) => void) => any): ThenPromise<T>;
  52. /**
  53. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  54. * resolve, or rejected when any ThenPromise is rejected.
  55. * @param values An array of Promises.
  56. * @returns A new ThenPromise.
  57. */
  58. all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>, T10 | Thenable<T10>]): ThenPromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
  59. /**
  60. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  61. * resolve, or rejected when any ThenPromise is rejected.
  62. * @param values An array of Promises.
  63. * @returns A new ThenPromise.
  64. */
  65. all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>]): ThenPromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
  66. /**
  67. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  68. * resolve, or rejected when any ThenPromise is rejected.
  69. * @param values An array of Promises.
  70. * @returns A new ThenPromise.
  71. */
  72. all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>]): ThenPromise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
  73. /**
  74. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  75. * resolve, or rejected when any ThenPromise is rejected.
  76. * @param values An array of Promises.
  77. * @returns A new ThenPromise.
  78. */
  79. all<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>]): ThenPromise<[T1, T2, T3, T4, T5, T6, T7]>;
  80. /**
  81. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  82. * resolve, or rejected when any ThenPromise is rejected.
  83. * @param values An array of Promises.
  84. * @returns A new ThenPromise.
  85. */
  86. all<T1, T2, T3, T4, T5, T6>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>, T6 | Thenable<T6>]): ThenPromise<[T1, T2, T3, T4, T5, T6]>;
  87. /**
  88. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  89. * resolve, or rejected when any ThenPromise is rejected.
  90. * @param values An array of Promises.
  91. * @returns A new ThenPromise.
  92. */
  93. all<T1, T2, T3, T4, T5>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>, T5 | Thenable<T5>]): ThenPromise<[T1, T2, T3, T4, T5]>;
  94. /**
  95. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  96. * resolve, or rejected when any ThenPromise is rejected.
  97. * @param values An array of Promises.
  98. * @returns A new ThenPromise.
  99. */
  100. all<T1, T2, T3, T4>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable <T4>]): ThenPromise<[T1, T2, T3, T4]>;
  101. /**
  102. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  103. * resolve, or rejected when any ThenPromise is rejected.
  104. * @param values An array of Promises.
  105. * @returns A new ThenPromise.
  106. */
  107. all<T1, T2, T3>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>]): ThenPromise<[T1, T2, T3]>;
  108. /**
  109. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  110. * resolve, or rejected when any ThenPromise is rejected.
  111. * @param values An array of Promises.
  112. * @returns A new ThenPromise.
  113. */
  114. all<T1, T2>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>]): ThenPromise<[T1, T2]>;
  115. /**
  116. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  117. * resolve, or rejected when any ThenPromise is rejected.
  118. * @param values An array of Promises.
  119. * @returns A new ThenPromise.
  120. */
  121. all<T>(values: (T | Thenable<T>)[]): ThenPromise<T[]>;
  122. /**
  123. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  124. * or rejected.
  125. * @param values An array of Promises.
  126. * @returns A new ThenPromise.
  127. */
  128. race<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable<T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>, T10 | Thenable<T10>]): ThenPromise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10>;
  129. /**
  130. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  131. * or rejected.
  132. * @param values An array of Promises.
  133. * @returns A new ThenPromise.
  134. */
  135. race<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable<T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>, T9 | Thenable<T9>]): ThenPromise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
  136. /**
  137. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  138. * or rejected.
  139. * @param values An array of Promises.
  140. * @returns A new ThenPromise.
  141. */
  142. race<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable<T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>, T8 | Thenable<T8>]): ThenPromise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
  143. /**
  144. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  145. * or rejected.
  146. * @param values An array of Promises.
  147. * @returns A new ThenPromise.
  148. */
  149. race<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable<T4>, T5 | Thenable<T5>, T6 | Thenable<T6>, T7 | Thenable<T7>]): ThenPromise<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
  150. /**
  151. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  152. * or rejected.
  153. * @param values An array of Promises.
  154. * @returns A new ThenPromise.
  155. */
  156. race<T1, T2, T3, T4, T5, T6>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable<T4>, T5 | Thenable<T5>, T6 | Thenable<T6>]): ThenPromise<T1 | T2 | T3 | T4 | T5 | T6>;
  157. /**
  158. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  159. * or rejected.
  160. * @param values An array of Promises.
  161. * @returns A new ThenPromise.
  162. */
  163. race<T1, T2, T3, T4, T5>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable<T4>, T5 | Thenable<T5>]): ThenPromise<T1 | T2 | T3 | T4 | T5>;
  164. /**
  165. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  166. * or rejected.
  167. * @param values An array of Promises.
  168. * @returns A new ThenPromise.
  169. */
  170. race<T1, T2, T3, T4>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>, T4 | Thenable<T4>]): ThenPromise<T1 | T2 | T3 | T4>;
  171. /**
  172. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  173. * or rejected.
  174. * @param values An array of Promises.
  175. * @returns A new ThenPromise.
  176. */
  177. race<T1, T2, T3>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>, T3 | Thenable<T3>]): ThenPromise<T1 | T2 | T3>;
  178. /**
  179. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  180. * or rejected.
  181. * @param values An array of Promises.
  182. * @returns A new ThenPromise.
  183. */
  184. race<T1, T2>(values: [T1 | Thenable<T1>, T2 | Thenable<T2>]): ThenPromise<T1 | T2>;
  185. /**
  186. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  187. * or rejected.
  188. * @param values An array of Promises.
  189. * @returns A new ThenPromise.
  190. */
  191. race<T>(values: (T | Thenable<T>)[]): ThenPromise<T>;
  192. /**
  193. * Creates a new rejected promise for the provided reason.
  194. * @param reason The reason the promise was rejected.
  195. * @returns A new rejected ThenPromise.
  196. */
  197. reject(reason: any): ThenPromise<never>;
  198. /**
  199. * Creates a new rejected promise for the provided reason.
  200. * @param reason The reason the promise was rejected.
  201. * @returns A new rejected ThenPromise.
  202. */
  203. reject<T>(reason: any): ThenPromise<T>;
  204. /**
  205. * Creates a new resolved promise for the provided value.
  206. * @param value A promise.
  207. * @returns A promise whose internal state matches the provided promise.
  208. */
  209. resolve<T>(value: T | Thenable<T>): ThenPromise<T>;
  210. /**
  211. * Creates a new resolved promise .
  212. * @returns A resolved promise.
  213. */
  214. resolve(): ThenPromise<void>;
  215. // Extensions specific to then/promise
  216. denodeify: (fn: Function) => (...args: any[]) => ThenPromise<any>;
  217. nodeify: (fn: Function) => Function;
  218. }
  219. declare var ThenPromise: ThenPromiseConstructor;
  220. export = ThenPromise;