UNPKG

18.4 kBTypeScriptView Raw
1/**
2 * Represents the completion of an asynchronous operation
3 */
4interface 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 /**
14 * Attaches a callback for only the rejection of the ThenPromise.
15 * @param onrejected The callback to execute when the ThenPromise is rejected.
16 * @returns A ThenPromise for the completion of the callback.
17 */
18 catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null): ThenPromise<T | TResult>;
19
20 // Extensions specific to then/promise
21
22 /**
23 * Attaches callbacks for the resolution and/or rejection of the ThenPromise, without returning a new promise.
24 * @param onfulfilled The callback to execute when the ThenPromise is resolved.
25 * @param onrejected The callback to execute when the ThenPromise is rejected.
26 */
27 done(onfulfilled?: ((value: T) => any) | undefined | null, onrejected?: ((reason: any) => any) | undefined | null): void;
28
29
30 /**
31 * Calls a node.js style callback. If none is provided, the promise is returned.
32 */
33 nodeify(callback: void | null): ThenPromise<T>;
34 nodeify(callback: (err: Error, value: T) => void): void;
35}
36
37interface PromiseFulfilledResult<T> {
38 status: "fulfilled";
39 value: T;
40}
41
42interface PromiseRejectedResult {
43 status: "rejected";
44 reason: any;
45}
46
47type PromiseSettledResult<T> = PromiseFulfilledResult<T> | PromiseRejectedResult;
48
49interface ThenPromiseConstructor {
50 /**
51 * A reference to the prototype.
52 */
53 readonly prototype: ThenPromise<any>;
54
55 /**
56 * Creates a new ThenPromise.
57 * @param executor A callback used to initialize the promise. This callback is passed two arguments:
58 * a resolve callback used resolve the promise with a value or the result of another promise,
59 * and a reject callback used to reject the promise with a provided reason or error.
60 */
61 new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => any): ThenPromise<T>;
62
63
64 /**
65 * 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.
66 * @param values An array or iterable of Promises.
67 * @returns A new Promise.
68 */
69 any<T extends readonly unknown[] | []>(values: T): Promise<Awaited<T[number]>>;
70
71 /**
72 * 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.
73 * @param values An array or iterable of Promises.
74 * @returns A new Promise.
75 */
76 any<T>(values: Iterable<T | PromiseLike<T>>): Promise<Awaited<T>>
77
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, 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>]>;
85
86 /**
87 * Creates a Promise that is resolved with an array of results when all
88 * of the provided Promises resolve or reject.
89 * @param values An array of Promises.
90 * @returns A new Promise.
91 */
92 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>]>;
93
94 /**
95 * Creates a Promise that is resolved with an array of results when all
96 * of the provided Promises resolve or reject.
97 * @param values An array of Promises.
98 * @returns A new Promise.
99 */
100 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>]>;
101
102 /**
103 * Creates a Promise that is resolved with an array of results when all
104 * of the provided Promises resolve or reject.
105 * @param values An array of Promises.
106 * @returns A new Promise.
107 */
108 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>]>;
109
110 /**
111 * Creates a Promise that is resolved with an array of results when all
112 * of the provided Promises resolve or reject.
113 * @param values An array of Promises.
114 * @returns A new Promise.
115 */
116 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>]>;
117
118 /**
119 * Creates a Promise that is resolved with an array of results when all
120 * of the provided Promises resolve or reject.
121 * @param values An array of Promises.
122 * @returns A new Promise.
123 */
124 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>]>;
125
126 /**
127 * Creates a Promise that is resolved with an array of results when all
128 * of the provided Promises resolve or reject.
129 * @param values An array of Promises.
130 * @returns A new Promise.
131 */
132 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>]>;
133
134 /**
135 * Creates a Promise that is resolved with an array of results when all
136 * of the provided Promises resolve or reject.
137 * @param values An array of Promises.
138 * @returns A new Promise.
139 */
140 allSettled<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>, PromiseSettledResult<T3>]>;
141
142 /**
143 * Creates a Promise that is resolved with an array of results when all
144 * of the provided Promises resolve or reject.
145 * @param values An array of Promises.
146 * @returns A new Promise.
147 */
148 allSettled<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): ThenPromise<[PromiseSettledResult<T1>, PromiseSettledResult<T2>]>;
149
150 /**
151 * Creates a Promise that is resolved with an array of results when all
152 * of the provided Promises resolve or reject.
153 * @param values An array of Promises.
154 * @returns A new Promise.
155 */
156 allSettled<T>(values: (T | PromiseLike<T>)[]): ThenPromise<PromiseSettledResult<T>[]>;
157
158 /**
159 * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
160 * resolve, or rejected when any ThenPromise is rejected.
161 * @param values An array of Promises.
162 * @returns A new ThenPromise.
163 */
164 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]>;
165
166 /**
167 * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
168 * resolve, or rejected when any ThenPromise is rejected.
169 * @param values An array of Promises.
170 * @returns A new ThenPromise.
171 */
172 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]>;
173
174 /**
175 * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
176 * resolve, or rejected when any ThenPromise is rejected.
177 * @param values An array of Promises.
178 * @returns A new ThenPromise.
179 */
180 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]>;
181
182 /**
183 * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
184 * resolve, or rejected when any ThenPromise is rejected.
185 * @param values An array of Promises.
186 * @returns A new ThenPromise.
187 */
188 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]>;
189
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, 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]>;
197
198 /**
199 * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
200 * resolve, or rejected when any ThenPromise is rejected.
201 * @param values An array of Promises.
202 * @returns A new ThenPromise.
203 */
204 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]>;
205
206 /**
207 * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
208 * resolve, or rejected when any ThenPromise is rejected.
209 * @param values An array of Promises.
210 * @returns A new ThenPromise.
211 */
212 all<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike <T4>]): ThenPromise<[T1, T2, T3, T4]>;
213
214 /**
215 * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
216 * resolve, or rejected when any ThenPromise is rejected.
217 * @param values An array of Promises.
218 * @returns A new ThenPromise.
219 */
220 all<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): ThenPromise<[T1, T2, T3]>;
221
222 /**
223 * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
224 * resolve, or rejected when any ThenPromise is rejected.
225 * @param values An array of Promises.
226 * @returns A new ThenPromise.
227 */
228 all<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): ThenPromise<[T1, T2]>;
229
230 /**
231 * Creates a ThenPromise that is resolved with an array of results when all of the provided Promises
232 * resolve, or rejected when any ThenPromise is rejected.
233 * @param values An array of Promises.
234 * @returns A new ThenPromise.
235 */
236 all<T>(values: (T | PromiseLike<T>)[]): ThenPromise<T[]>;
237
238 /**
239 * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
240 * or rejected.
241 * @param values An array of Promises.
242 * @returns A new ThenPromise.
243 */
244 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>;
245
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, 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>;
253
254 /**
255 * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
256 * or rejected.
257 * @param values An array of Promises.
258 * @returns A new ThenPromise.
259 */
260 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>;
261
262 /**
263 * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
264 * or rejected.
265 * @param values An array of Promises.
266 * @returns A new ThenPromise.
267 */
268 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>;
269
270 /**
271 * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
272 * or rejected.
273 * @param values An array of Promises.
274 * @returns A new ThenPromise.
275 */
276 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>;
277
278 /**
279 * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
280 * or rejected.
281 * @param values An array of Promises.
282 * @returns A new ThenPromise.
283 */
284 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>;
285
286 /**
287 * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
288 * or rejected.
289 * @param values An array of Promises.
290 * @returns A new ThenPromise.
291 */
292 race<T1, T2, T3, T4>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>, T4 | PromiseLike<T4>]): ThenPromise<T1 | T2 | T3 | T4>;
293
294 /**
295 * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
296 * or rejected.
297 * @param values An array of Promises.
298 * @returns A new ThenPromise.
299 */
300 race<T1, T2, T3>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>, T3 | PromiseLike<T3>]): ThenPromise<T1 | T2 | T3>;
301
302 /**
303 * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
304 * or rejected.
305 * @param values An array of Promises.
306 * @returns A new ThenPromise.
307 */
308 race<T1, T2>(values: [T1 | PromiseLike<T1>, T2 | PromiseLike<T2>]): ThenPromise<T1 | T2>;
309
310 /**
311 * Creates a ThenPromise that is resolved or rejected when any of the provided Promises are resolved
312 * or rejected.
313 * @param values An array of Promises.
314 * @returns A new ThenPromise.
315 */
316 race<T>(values: (T | PromiseLike<T>)[]): ThenPromise<T>;
317
318 /**
319 * Creates a new rejected promise for the provided reason.
320 * @param reason The reason the promise was rejected.
321 * @returns A new rejected ThenPromise.
322 */
323 reject(reason: any): ThenPromise<never>;
324
325 /**
326 * Creates a new rejected promise for the provided reason.
327 * @param reason The reason the promise was rejected.
328 * @returns A new rejected ThenPromise.
329 */
330 reject<T>(reason: any): ThenPromise<T>;
331
332 /**
333 * Creates a new resolved promise for the provided value.
334 * @param value A promise.
335 * @returns A promise whose internal state matches the provided promise.
336 */
337 resolve<T>(value: T | PromiseLike<T>): ThenPromise<T>;
338
339 /**
340 * Creates a new resolved promise .
341 * @returns A resolved promise.
342 */
343 resolve(): ThenPromise<void>;
344
345 // Extensions specific to then/promise
346
347 denodeify: (fn: Function) => (...args: any[]) => ThenPromise<any>;
348 nodeify: (fn: Function) => Function;
349}
350
351declare var ThenPromise: ThenPromiseConstructor;
352
353export = ThenPromise;