index.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**
  2. * Represents the completion of an asynchronous operation
  3. */
  4. interface ThenPromise<T> extends Promise<T> {
  5. /**
  6. * Attaches callbacks for the resolution and/or rejection of the ThenPromise.
  7. * @param onfulfilled The callback to execute when the ThenPromise is resolved.
  8. * @param onrejected The callback to execute when the ThenPromise is rejected.
  9. * @returns A ThenPromise for the completion of which ever callback is executed.
  10. */
  11. then<TResult1 = T, TResult2 = never>(onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null): ThenPromise<TResult1 | TResult2>;
  12. /**
  13. * Attaches a callback for only the rejection of the ThenPromise.
  14. * @param onrejected The callback to execute when the ThenPromise is rejected.
  15. * @returns A ThenPromise for the completion of the callback.
  16. */
  17. catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): ThenPromise<T | TResult>;
  18. // Extensions specific to then/promise
  19. /**
  20. * Attaches callbacks for the resolution and/or rejection of the ThenPromise, without returning a new promise.
  21. * @param onfulfilled The callback to execute when the ThenPromise is resolved.
  22. * @param onrejected The callback to execute when the ThenPromise is rejected.
  23. */
  24. done(onfulfilled?: ((value: T) => any) | undefined | null, onrejected?: ((reason: any) => any) | undefined | null): void;
  25. /**
  26. * Calls a node.js style callback. If none is provided, the promise is returned.
  27. */
  28. nodeify(callback: void | null): ThenPromise<T>;
  29. nodeify(callback: (err: Error, value: T) => void): void;
  30. }
  31. interface PromiseFulfilledResult<T> {
  32. status: "fulfilled";
  33. value: T;
  34. }
  35. interface PromiseRejectedResult {
  36. status: "rejected";
  37. reason: any;
  38. }
  39. type PromiseSettledResult<T> = PromiseFulfilledResult<T> | PromiseRejectedResult;
  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 | PromiseLike<T>) => void, reject: (reason?: any) => void) => any): ThenPromise<T>;
  52. /**
  53. * The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
  54. * @param values An array or iterable of Promises.
  55. * @returns A new Promise.
  56. */
  57. any<T extends readonly unknown[] | []>(values: T): Promise<Awaited<T[number]>>;
  58. /**
  59. * The any function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an AggregateError containing an array of rejection reasons if all of the given promises are rejected. It resolves all elements of the passed iterable to promises as it runs this algorithm.
  60. * @param values An array or iterable of Promises.
  61. * @returns A new Promise.
  62. */
  63. any<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>
  64. /**
  65. * Creates a Promise that is resolved with an array of results when all
  66. * of the provided Promises resolve or reject.
  67. * @param values An array of Promises.
  68. * @returns A new Promise.
  69. */
  70. allSettled<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>, PromiseSettledResult<T6>, PromiseSettledResult<T7>, PromiseSettledResult<T8>, PromiseSettledResult<T9>, PromiseSettledResult<T10>]>;
  71. /**
  72. * Creates a Promise that is resolved with an array of results when all
  73. * of the provided Promises resolve or reject.
  74. * @param values An array of Promises.
  75. * @returns A new Promise.
  76. */
  77. allSettled<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>, PromiseSettledResult<T6>, PromiseSettledResult<T7>, PromiseSettledResult<T8>, PromiseSettledResult<T9>]>;
  78. /**
  79. * Creates a Promise that is resolved with an array of results when all
  80. * of the provided Promises resolve or reject.
  81. * @param values An array of Promises.
  82. * @returns A new Promise.
  83. */
  84. allSettled<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>, PromiseSettledResult<T6>, PromiseSettledResult<T7>, PromiseSettledResult<T8>]>;
  85. /**
  86. * Creates a Promise that is resolved with an array of results when all
  87. * of the provided Promises resolve or reject.
  88. * @param values An array of Promises.
  89. * @returns A new Promise.
  90. */
  91. allSettled<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>, PromiseSettledResult<T6>, PromiseSettledResult<T7>]>;
  92. /**
  93. * Creates a Promise that is resolved with an array of results when all
  94. * of the provided Promises resolve or reject.
  95. * @param values An array of Promises.
  96. * @returns A new Promise.
  97. */
  98. allSettled<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>, PromiseSettledResult<T6>]>;
  99. /**
  100. * Creates a Promise that is resolved with an array of results when all
  101. * of the provided Promises resolve or reject.
  102. * @param values An array of Promises.
  103. * @returns A new Promise.
  104. */
  105. allSettled<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>, PromiseSettledResult<T5>]>;
  106. /**
  107. * Creates a Promise that is resolved with an array of results when all
  108. * of the provided Promises resolve or reject.
  109. * @param values An array of Promises.
  110. * @returns A new Promise.
  111. */
  112. allSettled<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>, PromiseSettledResult<T4>]>;
  113. /**
  114. * Creates a Promise that is resolved with an array of results when all
  115. * of the provided Promises resolve or reject.
  116. * @param values An array of Promises.
  117. * @returns A new Promise.
  118. */
  119. allSettled<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>]>;
  120. /**
  121. * Creates a Promise that is resolved with an array of results when all
  122. * of the provided Promises resolve or reject.
  123. * @param values An array of Promises.
  124. * @returns A new Promise.
  125. */
  126. allSettled<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>]>;
  127. /**
  128. * Creates a Promise that is resolved with an array of results when all
  129. * of the provided Promises resolve or reject.
  130. * @param values An array of Promises.
  131. * @returns A new Promise.
  132. */
  133. allSettled<T>(values: (T | PromiseLike<T>)[]): ThenPromise<PromiseSettledResult<T>[]>;
  134. /**
  135. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  136. * resolve, or rejected when any ThenPromise is rejected.
  137. * @param values An array of Promises.
  138. * @returns A new ThenPromise.
  139. */
  140. all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): ThenPromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
  141. /**
  142. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  143. * resolve, or rejected when any ThenPromise is rejected.
  144. * @param values An array of Promises.
  145. * @returns A new ThenPromise.
  146. */
  147. all<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): ThenPromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
  148. /**
  149. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  150. * resolve, or rejected when any ThenPromise is rejected.
  151. * @param values An array of Promises.
  152. * @returns A new ThenPromise.
  153. */
  154. all<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): ThenPromise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
  155. /**
  156. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  157. * resolve, or rejected when any ThenPromise is rejected.
  158. * @param values An array of Promises.
  159. * @returns A new ThenPromise.
  160. */
  161. all<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): ThenPromise<[T1, T2, T3, T4, T5, T6, T7]>;
  162. /**
  163. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  164. * resolve, or rejected when any ThenPromise is rejected.
  165. * @param values An array of Promises.
  166. * @returns A new ThenPromise.
  167. */
  168. all<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): ThenPromise<[T1, T2, T3, T4, T5, T6]>;
  169. /**
  170. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  171. * resolve, or rejected when any ThenPromise is rejected.
  172. * @param values An array of Promises.
  173. * @returns A new ThenPromise.
  174. */
  175. all<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>, T5 | PromiseLike<T5>]): ThenPromise<[T1, T2, T3, T4, T5]>;
  176. /**
  177. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  178. * resolve, or rejected when any ThenPromise is rejected.
  179. * @param values An array of Promises.
  180. * @returns A new ThenPromise.
  181. */
  182. all<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>]): ThenPromise<[T1, T2, T3, T4]>;
  183. /**
  184. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  185. * resolve, or rejected when any ThenPromise is rejected.
  186. * @param values An array of Promises.
  187. * @returns A new ThenPromise.
  188. */
  189. all<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): ThenPromise<[T1, T2, T3]>;
  190. /**
  191. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  192. * resolve, or rejected when any ThenPromise is rejected.
  193. * @param values An array of Promises.
  194. * @returns A new ThenPromise.
  195. */
  196. all<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): ThenPromise<[T1, T2]>;
  197. /**
  198. * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
  199. * resolve, or rejected when any ThenPromise is rejected.
  200. * @param values An array of Promises.
  201. * @returns A new ThenPromise.
  202. */
  203. all<T>(values: (T | PromiseLike<T>)[]): ThenPromise<T[]>;
  204. /**
  205. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  206. * or rejected.
  207. * @param values An array of Promises.
  208. * @returns A new ThenPromise.
  209. */
  210. race<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>, T10 | PromiseLike<T10>]): ThenPromise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9 | T10>;
  211. /**
  212. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  213. * or rejected.
  214. * @param values An array of Promises.
  215. * @returns A new ThenPromise.
  216. */
  217. race<T1, T2, T3, T4, T5, T6, T7, T8, T9>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>, T9 | PromiseLike<T9>]): ThenPromise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9>;
  218. /**
  219. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  220. * or rejected.
  221. * @param values An array of Promises.
  222. * @returns A new ThenPromise.
  223. */
  224. race<T1, T2, T3, T4, T5, T6, T7, T8>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>, T8 | PromiseLike<T8>]): ThenPromise<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8>;
  225. /**
  226. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  227. * or rejected.
  228. * @param values An array of Promises.
  229. * @returns A new ThenPromise.
  230. */
  231. race<T1, T2, T3, T4, T5, T6, T7>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>, T7 | PromiseLike<T7>]): ThenPromise<T1 | T2 | T3 | T4 | T5 | T6 | T7>;
  232. /**
  233. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  234. * or rejected.
  235. * @param values An array of Promises.
  236. * @returns A new ThenPromise.
  237. */
  238. race<T1, T2, T3, T4, T5, T6>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>, T6 | PromiseLike<T6>]): ThenPromise<T1 | T2 | T3 | T4 | T5 | T6>;
  239. /**
  240. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  241. * or rejected.
  242. * @param values An array of Promises.
  243. * @returns A new ThenPromise.
  244. */
  245. race<T1, T2, T3, T4, T5>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>, T5 | PromiseLike<T5>]): ThenPromise<T1 | T2 | T3 | T4 | T5>;
  246. /**
  247. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  248. * or rejected.
  249. * @param values An array of Promises.
  250. * @returns A new ThenPromise.
  251. */
  252. race<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): ThenPromise<T1 | T2 | T3 | T4>;
  253. /**
  254. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  255. * or rejected.
  256. * @param values An array of Promises.
  257. * @returns A new ThenPromise.
  258. */
  259. race<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): ThenPromise<T1 | T2 | T3>;
  260. /**
  261. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  262. * or rejected.
  263. * @param values An array of Promises.
  264. * @returns A new ThenPromise.
  265. */
  266. race<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): ThenPromise<T1 | T2>;
  267. /**
  268. * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
  269. * or rejected.
  270. * @param values An array of Promises.
  271. * @returns A new ThenPromise.
  272. */
  273. race<T>(values: (T | PromiseLike<T>)[]): ThenPromise<T>;
  274. /**
  275. * Creates a new rejected promise for the provided reason.
  276. * @param reason The reason the promise was rejected.
  277. * @returns A new rejected ThenPromise.
  278. */
  279. reject(reason: any): ThenPromise<never>;
  280. /**
  281. * Creates a new rejected promise for the provided reason.
  282. * @param reason The reason the promise was rejected.
  283. * @returns A new rejected ThenPromise.
  284. */
  285. reject<T>(reason: any): ThenPromise<T>;
  286. /**
  287. * Creates a new resolved promise for the provided value.
  288. * @param value A promise.
  289. * @returns A promise whose internal state matches the provided promise.
  290. */
  291. resolve<T>(value: T | PromiseLike<T>): ThenPromise<T>;
  292. /**
  293. * Creates a new resolved promise .
  294. * @returns A resolved promise.
  295. */
  296. resolve(): ThenPromise<void>;
  297. // Extensions specific to then/promise
  298. denodeify: (fn: Function) => (...args: any[]) => ThenPromise<any>;
  299. nodeify: (fn: Function) => Function;
  300. }
  301. declare var ThenPromise: ThenPromiseConstructor;
  302. export = ThenPromise;