UNPKG

58.6 kBTypeScriptView Raw
1/*!
2 * The code following this comment originates from:
3 * https://github.com/types/npm-bluebird
4 *
5 * Note for browser users: use bluebird-global typings instead of this one
6 * if you want to use Bluebird via the global Promise symbol.
7 *
8 * Licensed under:
9 * The MIT License (MIT)
10 *
11 * Copyright (c) 2016 unional
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 * THE SOFTWARE.
30 */
31
32type Constructor<E> = new(...args: any[]) => E;
33type CatchFilter<E> = ((error: E) => boolean) | (object & E);
34type Resolvable<R> = R | PromiseLike<R>;
35type IterateFunction<T, R> = (item: T, index: number, arrayLength: number) => Resolvable<R>;
36
37type PromisifyAllKeys<T> = T extends string ? `${T}Async` : never;
38type WithoutLast<T> = T extends [...infer A, any] ? A : [];
39type Last<T> = T extends [...any[], infer L] ? L : never;
40type ExtractCallbackValueType<T> = T extends (error: any, ...data: infer D) => any ? D : never;
41
42type PromiseMethod<TArgs, TReturn> = TReturn extends never ? never : (...args: WithoutLast<TArgs>) => Promise<TReturn>;
43
44type ExtractAsyncMethod<T> = T extends (...args: infer A) => any
45 ? PromiseMethod<A, ExtractCallbackValueType<Last<Required<A>>>[0]>
46 : never;
47
48type PromisifyAllItems<T> = {
49 [K in keyof T as PromisifyAllKeys<K>]: ExtractAsyncMethod<T[K]>;
50};
51
52type NonNeverValues<T> = {
53 [K in keyof T as T[K] extends never ? never : K]: T[K];
54};
55
56// Drop `never` values
57type PromisifyAll<T> = NonNeverValues<PromisifyAllItems<T>> & T;
58
59declare class Bluebird<R> implements PromiseLike<R>, Bluebird.Inspection<R> {
60 readonly [Symbol.toStringTag]: "Object";
61
62 /**
63 * Create a new promise. The passed in function will receive functions
64 * `resolve` and `reject` as its arguments which can be called to seal the fate of the created promise.
65 *
66 * If promise cancellation is enabled, passed in function will receive
67 * one more function argument `onCancel` that allows to register an optional cancellation callback.
68 */
69 constructor(
70 callback: (
71 resolve: (thenableOrResult?: Resolvable<R>) => void,
72 reject: (error?: any) => void,
73 onCancel?: (callback: () => void) => void,
74 ) => void,
75 );
76
77 /**
78 * Promises/A+ `.then()`. Returns a new promise chained from this promise.
79 *
80 * The new promise will be rejected or resolved depending on the passed `fulfilledHandler`, `rejectedHandler` and the state of this promise.
81 */
82 // Based on PromiseLike.then, but returns a Bluebird instance.
83 then<U>(onFulfill?: (value: R) => Resolvable<U>, onReject?: (error: any) => Resolvable<U>): Bluebird<U>; // For simpler signature help.
84 then<TResult1 = R, TResult2 = never>(
85 onfulfilled?: ((value: R) => Resolvable<TResult1>) | null,
86 onrejected?: ((reason: any) => Resolvable<TResult2>) | null,
87 ): Bluebird<TResult1 | TResult2>;
88
89 /**
90 * This is a catch-all exception handler, shortcut for calling `.then(null, handler)` on this promise.
91 *
92 * Any exception happening in a `.then`-chain will propagate to nearest `.catch` handler.
93 *
94 * Alias `.caught();` for compatibility with earlier ECMAScript version.
95 */
96 catch<U = R>(onReject: ((error: any) => Resolvable<U>) | undefined | null): Bluebird<U | R>;
97
98 /**
99 * This extends `.catch` to work more like catch-clauses in languages like Java or C#.
100 *
101 * Instead of manually checking `instanceof` or `.name === "SomeError"`,
102 * you may specify a number of error constructors which are eligible for this catch handler.
103 * The catch handler that is first met that has eligible constructors specified, is the one that will be called.
104 *
105 * This method also supports predicate-based filters.
106 * If you pass a predicate function instead of an error constructor, the predicate will receive the error as an argument.
107 * The return result of the predicate will be used determine whether the error handler should be called.
108 *
109 * Alias `.caught();` for compatibility with earlier ECMAScript version.
110 */
111 catch<U, E1, E2, E3, E4, E5>(
112 filter1: Constructor<E1>,
113 filter2: Constructor<E2>,
114 filter3: Constructor<E3>,
115 filter4: Constructor<E4>,
116 filter5: Constructor<E5>,
117 onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable<U>,
118 ): Bluebird<U | R>;
119
120 catch<U, E1, E2, E3, E4, E5>(
121 filter1: Constructor<E1> | CatchFilter<E1>,
122 filter2: Constructor<E2> | CatchFilter<E2>,
123 filter3: Constructor<E3> | CatchFilter<E3>,
124 filter4: Constructor<E4> | CatchFilter<E4>,
125 filter5: Constructor<E5> | CatchFilter<E5>,
126 onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable<U>,
127 ): Bluebird<U | R>;
128
129 catch<U, E1, E2, E3, E4>(
130 filter1: Constructor<E1>,
131 filter2: Constructor<E2>,
132 filter3: Constructor<E3>,
133 filter4: Constructor<E4>,
134 onReject: (error: E1 | E2 | E3 | E4) => Resolvable<U>,
135 ): Bluebird<U | R>;
136
137 catch<U, E1, E2, E3, E4>(
138 filter1: Constructor<E1> | CatchFilter<E1>,
139 filter2: Constructor<E2> | CatchFilter<E2>,
140 filter3: Constructor<E3> | CatchFilter<E3>,
141 filter4: Constructor<E4> | CatchFilter<E4>,
142 onReject: (error: E1 | E2 | E3 | E4) => Resolvable<U>,
143 ): Bluebird<U | R>;
144
145 catch<U, E1, E2, E3>(
146 filter1: Constructor<E1>,
147 filter2: Constructor<E2>,
148 filter3: Constructor<E3>,
149 onReject: (error: E1 | E2 | E3) => Resolvable<U>,
150 ): Bluebird<U | R>;
151
152 catch<U, E1, E2, E3>(
153 filter1: Constructor<E1> | CatchFilter<E1>,
154 filter2: Constructor<E2> | CatchFilter<E2>,
155 filter3: Constructor<E3> | CatchFilter<E3>,
156 onReject: (error: E1 | E2 | E3) => Resolvable<U>,
157 ): Bluebird<U | R>;
158
159 catch<U, E1, E2>(
160 filter1: Constructor<E1>,
161 filter2: Constructor<E2>,
162 onReject: (error: E1 | E2) => Resolvable<U>,
163 ): Bluebird<U | R>;
164
165 catch<U, E1, E2>(
166 filter1: Constructor<E1> | CatchFilter<E1>,
167 filter2: Constructor<E2> | CatchFilter<E2>,
168 onReject: (error: E1 | E2) => Resolvable<U>,
169 ): Bluebird<U | R>;
170
171 catch<U, E1>(
172 filter1: Constructor<E1>,
173 onReject: (error: E1) => Resolvable<U>,
174 ): Bluebird<U | R>;
175
176 catch<U, E1>(
177 // tslint:disable-next-line:unified-signatures
178 filter1: Constructor<E1> | CatchFilter<E1>,
179 onReject: (error: E1) => Resolvable<U>,
180 ): Bluebird<U | R>;
181
182 /**
183 * This is a catch-all exception handler, shortcut for calling `.then(null, handler)` on this promise.
184 *
185 * Any exception happening in a `.then`-chain will propagate to nearest `.catch` handler.
186 *
187 * Alias `.caught();` for compatibility with earlier ECMAScript version.
188 */
189 caught: Bluebird<R>["catch"];
190
191 /**
192 * Like `.catch` but instead of catching all types of exceptions,
193 * it only catches those that don't originate from thrown errors but rather from explicit rejections.
194 */
195 error<U>(onReject: (reason: any) => Resolvable<U>): Bluebird<U>;
196
197 /**
198 * Pass a handler that will be called regardless of this promise's fate. Returns a new promise chained from this promise.
199 *
200 * There are special semantics for `.finally()` in that the final value cannot be modified from the handler.
201 *
202 * Alias `.lastly();` for compatibility with earlier ECMAScript version.
203 */
204 finally(handler: () => Resolvable<any>): Bluebird<R>;
205
206 lastly: Bluebird<R>["finally"];
207
208 /**
209 * Create a promise that follows this promise, but is bound to the given `thisArg` value.
210 * A bound promise will call its handlers with the bound value set to `this`.
211 *
212 * Additionally promises derived from a bound promise will also be bound promises with the same `thisArg` binding as the original promise.
213 */
214 bind(thisArg: any): Bluebird<R>;
215
216 /**
217 * Like `.then()`, but any unhandled rejection that ends up here will be thrown as an error.
218 */
219 done<U>(onFulfilled?: (value: R) => Resolvable<U>, onRejected?: (error: any) => Resolvable<U>): void;
220
221 /**
222 * Like `.finally()`, but not called for rejections.
223 */
224 tap(onFulFill: (value: R) => Resolvable<any>): Bluebird<R>;
225
226 /**
227 * Like `.catch()` but rethrows the error
228 */
229 tapCatch(onReject: (error?: any) => Resolvable<any>): Bluebird<R>;
230
231 tapCatch<E1, E2, E3, E4, E5>(
232 filter1: Constructor<E1>,
233 filter2: Constructor<E2>,
234 filter3: Constructor<E3>,
235 filter4: Constructor<E4>,
236 filter5: Constructor<E5>,
237 onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable<any>,
238 ): Bluebird<R>;
239 tapCatch<E1, E2, E3, E4, E5>(
240 filter1: Constructor<E1> | CatchFilter<E1>,
241 filter2: Constructor<E2> | CatchFilter<E2>,
242 filter3: Constructor<E3> | CatchFilter<E3>,
243 filter4: Constructor<E4> | CatchFilter<E4>,
244 filter5: Constructor<E5> | CatchFilter<E5>,
245 onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable<any>,
246 ): Bluebird<R>;
247 tapCatch<E1, E2, E3, E4>(
248 filter1: Constructor<E1>,
249 filter2: Constructor<E2>,
250 filter3: Constructor<E3>,
251 filter4: Constructor<E4>,
252 onReject: (error: E1 | E2 | E3 | E4) => Resolvable<any>,
253 ): Bluebird<R>;
254 tapCatch<E1, E2, E3, E4>(
255 filter1: Constructor<E1> | CatchFilter<E1>,
256 filter2: Constructor<E2> | CatchFilter<E2>,
257 filter3: Constructor<E3> | CatchFilter<E3>,
258 filter4: Constructor<E4> | CatchFilter<E4>,
259 onReject: (error: E1 | E2 | E3 | E4) => Resolvable<any>,
260 ): Bluebird<R>;
261 tapCatch<E1, E2, E3>(
262 filter1: Constructor<E1>,
263 filter2: Constructor<E2>,
264 filter3: Constructor<E3>,
265 onReject: (error: E1 | E2 | E3) => Resolvable<any>,
266 ): Bluebird<R>;
267 tapCatch<E1, E2, E3>(
268 filter1: Constructor<E1> | CatchFilter<E1>,
269 filter2: Constructor<E2> | CatchFilter<E2>,
270 filter3: Constructor<E3> | CatchFilter<E3>,
271 onReject: (error: E1 | E2 | E3) => Resolvable<any>,
272 ): Bluebird<R>;
273 tapCatch<E1, E2>(
274 filter1: Constructor<E1>,
275 filter2: Constructor<E2>,
276 onReject: (error: E1 | E2) => Resolvable<any>,
277 ): Bluebird<R>;
278 tapCatch<E1, E2>(
279 filter1: Constructor<E1> | CatchFilter<E1>,
280 filter2: Constructor<E2> | CatchFilter<E2>,
281 onReject: (error: E1 | E2) => Resolvable<any>,
282 ): Bluebird<R>;
283 tapCatch<E1>(
284 filter1: Constructor<E1>,
285 onReject: (error: E1) => Resolvable<any>,
286 ): Bluebird<R>;
287 tapCatch<E1>(
288 // tslint:disable-next-line:unified-signatures
289 filter1: Constructor<E1> | CatchFilter<E1>,
290 onReject: (error: E1) => Resolvable<any>,
291 ): Bluebird<R>;
292
293 /**
294 * Same as calling `Promise.delay(ms, this)`.
295 */
296 delay(ms: number): Bluebird<R>;
297
298 /**
299 * Returns a promise that will be fulfilled with this promise's fulfillment value or rejection reason.
300 * However, if this promise is not fulfilled or rejected within ms milliseconds, the returned promise
301 * is rejected with a TimeoutError or the error as the reason.
302 *
303 * You may specify a custom error message with the `message` parameter.
304 */
305 timeout(ms: number, message?: string | Error): Bluebird<R>;
306
307 /**
308 * Register a node-style callback on this promise.
309 *
310 * When this promise is is either fulfilled or rejected,
311 * the node callback will be called back with the node.js convention
312 * where error reason is the first argument and success value is the second argument.
313 *
314 * The error argument will be `null` in case of success.
315 * If the `callback` argument is not a function, this method does not do anything.
316 */
317 nodeify(callback: (err: any, value?: R) => void, options?: Bluebird.SpreadOption): this;
318 nodeify(...sink: any[]): this;
319 asCallback(callback: (err: any, value?: R) => void, options?: Bluebird.SpreadOption): this;
320 asCallback(...sink: any[]): this;
321
322 /**
323 * See if this `promise` has been fulfilled.
324 */
325 isFulfilled(): boolean;
326
327 /**
328 * See if this `promise` has been rejected.
329 */
330 isRejected(): boolean;
331
332 /**
333 * See if this `promise` is still defer.
334 */
335 isPending(): boolean;
336
337 /**
338 * See if this `promise` has been cancelled.
339 */
340 isCancelled(): boolean;
341
342 /**
343 * See if this `promise` is resolved -> either fulfilled or rejected.
344 */
345 isResolved(): boolean;
346
347 /**
348 * Get the fulfillment value of the underlying promise. Throws if the promise isn't fulfilled yet.
349 *
350 * throws `TypeError`
351 */
352 value(): R;
353
354 /**
355 * Get the rejection reason for the underlying promise. Throws if the promise isn't rejected yet.
356 *
357 * throws `TypeError`
358 */
359 reason(): any;
360
361 /**
362 * Synchronously inspect the state of this `promise`. The `PromiseInspection` will represent the state of
363 * the promise as snapshotted at the time of calling `.reflect()`.
364 */
365 reflect(): Bluebird<Bluebird.Inspection<R>>;
366
367 /**
368 * This is a convenience method for doing:
369 *
370 * <code>
371 * promise.then(function(obj){
372 * return obj[propertyName].call(obj, arg...);
373 * });
374 * </code>
375 */
376 call<U extends keyof Q, Q>(
377 this: Bluebird<Q>,
378 propertyName: U,
379 ...args: any[]
380 ): Bluebird<Q[U] extends (...args: any[]) => any ? ReturnType<Q[U]> : never>;
381
382 /**
383 * This is a convenience method for doing:
384 *
385 * <code>
386 * promise.then(function(obj){
387 * return obj[propertyName];
388 * });
389 * </code>
390 */
391 get<U extends keyof R>(key: U): Bluebird<R[U]>;
392
393 /**
394 * Convenience method for:
395 *
396 * <code>
397 * .then(function() {
398 * return value;
399 * });
400 * </code>
401 *
402 * in the case where `value` doesn't change its value. That means `value` is bound at the time of calling `.return()`
403 *
404 * Alias `.thenReturn();` for compatibility with earlier ECMAScript version.
405 */
406 return(): Bluebird<void>;
407 return<U>(value: U): Bluebird<U>;
408 thenReturn(): Bluebird<void>;
409 thenReturn<U>(value: U): Bluebird<U>;
410
411 /**
412 * Convenience method for:
413 *
414 * <code>
415 * .then(function() {
416 * throw reason;
417 * });
418 * </code>
419 * Same limitations apply as with `.return()`.
420 *
421 * Alias `.thenThrow();` for compatibility with earlier ECMAScript version.
422 */
423 throw(reason: Error): Bluebird<never>;
424 thenThrow(reason: Error): Bluebird<never>;
425
426 /**
427 * Convenience method for:
428 *
429 * <code>
430 * .catch(function() {
431 * return value;
432 * });
433 * </code>
434 *
435 * in the case where `value` doesn't change its value. That means `value` is bound at the time of calling `.catchReturn()`
436 */
437 catchReturn<U>(value: U): Bluebird<R | U>;
438
439 // No need to be specific about Error types in these overrides, since there's no handler function
440 catchReturn<U>(
441 filter1: Constructor<Error>,
442 filter2: Constructor<Error>,
443 filter3: Constructor<Error>,
444 filter4: Constructor<Error>,
445 filter5: Constructor<Error>,
446 value: U,
447 ): Bluebird<R | U>;
448 catchReturn<U>(
449 filter1: Constructor<Error> | CatchFilter<Error>,
450 filter2: Constructor<Error> | CatchFilter<Error>,
451 filter3: Constructor<Error> | CatchFilter<Error>,
452 filter4: Constructor<Error> | CatchFilter<Error>,
453 filter5: Constructor<Error> | CatchFilter<Error>,
454 value: U,
455 ): Bluebird<R | U>;
456 catchReturn<U>(
457 filter1: Constructor<Error>,
458 filter2: Constructor<Error>,
459 filter3: Constructor<Error>,
460 filter4: Constructor<Error>,
461 value: U,
462 ): Bluebird<R | U>;
463 catchReturn<U>(
464 filter1: Constructor<Error> | CatchFilter<Error>,
465 filter2: Constructor<Error> | CatchFilter<Error>,
466 filter3: Constructor<Error> | CatchFilter<Error>,
467 filter4: Constructor<Error> | CatchFilter<Error>,
468 value: U,
469 ): Bluebird<R | U>;
470 catchReturn<U>(
471 filter1: Constructor<Error>,
472 filter2: Constructor<Error>,
473 filter3: Constructor<Error>,
474 value: U,
475 ): Bluebird<R | U>;
476 catchReturn<U>(
477 filter1: Constructor<Error> | CatchFilter<Error>,
478 filter2: Constructor<Error> | CatchFilter<Error>,
479 filter3: Constructor<Error> | CatchFilter<Error>,
480 value: U,
481 ): Bluebird<R | U>;
482 catchReturn<U>(
483 filter1: Constructor<Error>,
484 filter2: Constructor<Error>,
485 value: U,
486 ): Bluebird<R | U>;
487 catchReturn<U>(
488 filter1: Constructor<Error> | CatchFilter<Error>,
489 filter2: Constructor<Error> | CatchFilter<Error>,
490 value: U,
491 ): Bluebird<R | U>;
492 catchReturn<U>(
493 filter1: Constructor<Error>,
494 value: U,
495 ): Bluebird<R | U>;
496 catchReturn<U>(
497 // tslint:disable-next-line:unified-signatures
498 filter1: Constructor<Error> | CatchFilter<Error>,
499 value: U,
500 ): Bluebird<R | U>;
501
502 /**
503 * Convenience method for:
504 *
505 * <code>
506 * .catch(function() {
507 * throw reason;
508 * });
509 * </code>
510 * Same limitations apply as with `.catchReturn()`.
511 */
512 catchThrow(reason: Error): Bluebird<R>;
513
514 // No need to be specific about Error types in these overrides, since there's no handler function
515 catchThrow(
516 filter1: Constructor<Error>,
517 filter2: Constructor<Error>,
518 filter3: Constructor<Error>,
519 filter4: Constructor<Error>,
520 filter5: Constructor<Error>,
521 reason: Error,
522 ): Bluebird<R>;
523 catchThrow(
524 filter1: Constructor<Error> | CatchFilter<Error>,
525 filter2: Constructor<Error> | CatchFilter<Error>,
526 filter3: Constructor<Error> | CatchFilter<Error>,
527 filter4: Constructor<Error> | CatchFilter<Error>,
528 filter5: Constructor<Error> | CatchFilter<Error>,
529 reason: Error,
530 ): Bluebird<R>;
531 catchThrow(
532 filter1: Constructor<Error>,
533 filter2: Constructor<Error>,
534 filter3: Constructor<Error>,
535 filter4: Constructor<Error>,
536 reason: Error,
537 ): Bluebird<R>;
538 catchThrow(
539 filter1: Constructor<Error> | CatchFilter<Error>,
540 filter2: Constructor<Error> | CatchFilter<Error>,
541 filter3: Constructor<Error> | CatchFilter<Error>,
542 filter4: Constructor<Error> | CatchFilter<Error>,
543 reason: Error,
544 ): Bluebird<R>;
545 catchThrow(
546 filter1: Constructor<Error>,
547 filter2: Constructor<Error>,
548 filter3: Constructor<Error>,
549 reason: Error,
550 ): Bluebird<R>;
551 catchThrow(
552 filter1: Constructor<Error> | CatchFilter<Error>,
553 filter2: Constructor<Error> | CatchFilter<Error>,
554 filter3: Constructor<Error> | CatchFilter<Error>,
555 reason: Error,
556 ): Bluebird<R>;
557 catchThrow(
558 filter1: Constructor<Error>,
559 filter2: Constructor<Error>,
560 reason: Error,
561 ): Bluebird<R>;
562 catchThrow(
563 filter1: Constructor<Error> | CatchFilter<Error>,
564 filter2: Constructor<Error> | CatchFilter<Error>,
565 reason: Error,
566 ): Bluebird<R>;
567 catchThrow(
568 filter1: Constructor<Error>,
569 reason: Error,
570 ): Bluebird<R>;
571 catchThrow(
572 // tslint:disable-next-line:unified-signatures
573 filter1: Constructor<Error> | CatchFilter<Error>,
574 reason: Error,
575 ): Bluebird<R>;
576
577 /**
578 * Convert to String.
579 */
580 toString(): string;
581
582 /**
583 * This is implicitly called by `JSON.stringify` when serializing the object. Returns a serialized representation of the `Promise`.
584 */
585 toJSON(): object;
586
587 /**
588 * Like calling `.then`, but the fulfillment value or rejection reason is assumed to be an array, which is flattened to the formal parameters of the handlers.
589 */
590 spread<U, Q>(this: Bluebird<R & Iterable<Q>>, fulfilledHandler: (...values: Q[]) => Resolvable<U>): Bluebird<U>;
591
592 /**
593 * Same as calling `Promise.all(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
594 */
595 all<T1, T2, T3, T4, T5>(
596 this: Bluebird<[Resolvable<T1>, Resolvable<T2>, Resolvable<T3>, Resolvable<T4>, Resolvable<T5>]>,
597 ): Bluebird<[T1, T2, T3, T4, T5]>;
598 all<T1, T2, T3, T4>(
599 this: Bluebird<[Resolvable<T1>, Resolvable<T2>, Resolvable<T3>, Resolvable<T4>]>,
600 ): Bluebird<[T1, T2, T3, T4]>;
601 all<T1, T2, T3>(this: Bluebird<[Resolvable<T1>, Resolvable<T2>, Resolvable<T3>]>): Bluebird<[T1, T2, T3]>;
602 all<T1, T2>(this: Bluebird<[Resolvable<T1>, Resolvable<T2>]>): Bluebird<[T1, T2]>;
603 all<T1>(this: Bluebird<[Resolvable<T1>]>): Bluebird<[T1]>;
604 all<R>(this: Bluebird<Iterable<Resolvable<R>>>): Bluebird<R[]>;
605
606 /**
607 * Same as calling `Promise.all(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
608 */
609 all(): Bluebird<never>;
610
611 /**
612 * Same as calling `Promise.props(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
613 */
614 props<K, V>(this: PromiseLike<Map<K, Resolvable<V>>>): Bluebird<Map<K, V>>;
615 props<T>(this: PromiseLike<Bluebird.ResolvableProps<T>>): Bluebird<T>;
616
617 /**
618 * Same as calling `Promise.any(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
619 */
620 any<Q>(this: Bluebird<R & Iterable<Q>>): Bluebird<Q>;
621
622 /**
623 * Same as calling `Promise.any(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
624 */
625 any(): Bluebird<never>;
626
627 /**
628 * Same as calling `Promise.some(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
629 * Same as calling `Promise.some(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
630 */
631 some<Q>(this: Bluebird<R & Iterable<Q>>, count: number): Bluebird<R>;
632
633 /**
634 * Same as calling `Promise.some(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
635 * Same as calling `Promise.some(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
636 */
637 some(count: number): Bluebird<never>;
638
639 /**
640 * Same as calling `Promise.race(thisPromise, count)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
641 */
642 race<Q>(this: Bluebird<R & Iterable<Q>>): Bluebird<Q>;
643
644 /**
645 * Same as calling `Promise.race(thisPromise, count)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
646 */
647 race(): Bluebird<never>;
648
649 /**
650 * Same as calling `Bluebird.map(thisPromise, mapper)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
651 */
652 map<U, Q>(
653 this: Bluebird<R & Iterable<Q>>,
654 mapper: IterateFunction<Q, U>,
655 options?: Bluebird.ConcurrencyOption,
656 ): Bluebird<U[]>;
657
658 /**
659 * Same as calling `Promise.reduce(thisPromise, Function reducer, initialValue)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
660 */
661 reduce<U, Q>(
662 this: Bluebird<R & Iterable<Q>>,
663 reducer: (memo: U, item: Q, index: number, arrayLength: number) => Resolvable<U>,
664 initialValue?: Resolvable<U>,
665 ): Bluebird<U>;
666
667 /**
668 * Same as calling ``Promise.filter(thisPromise, filterer)``. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
669 */
670 filter<Q>(
671 this: Bluebird<R & Iterable<Q>>,
672 filterer: IterateFunction<Q, boolean>,
673 options?: Bluebird.ConcurrencyOption,
674 ): Bluebird<R>;
675
676 /**
677 * Same as calling ``Bluebird.each(thisPromise, iterator)``. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
678 */
679 each<Q>(this: Bluebird<R & Iterable<Q>>, iterator: IterateFunction<Q, any>): Bluebird<R>;
680
681 /**
682 * Same as calling ``Bluebird.mapSeries(thisPromise, iterator)``. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
683 */
684 mapSeries<U, Q>(this: Bluebird<R & Iterable<Q>>, iterator: IterateFunction<Q, U>): Bluebird<U[]>;
685
686 /**
687 * Cancel this `promise`. Will not do anything if this promise is already settled or if the cancellation feature has not been enabled
688 */
689 cancel(): void;
690
691 /**
692 * Basically sugar for doing: somePromise.catch(function(){});
693 *
694 * Which is needed in case error handlers are attached asynchronously to the promise later, which would otherwise result in premature unhandled rejection reporting.
695 */
696 suppressUnhandledRejections(): void;
697
698 /**
699 * Start the chain of promises with `Promise.try`. Any synchronous exceptions will be turned into rejections on the returned promise.
700 *
701 * Note about second argument: if it's specifically a true array, its values become respective arguments for the function call.
702 * Otherwise it is passed as is as the first argument for the function call.
703 *
704 * Alias for `attempt();` for compatibility with earlier ECMAScript version.
705 */
706 static try<R>(fn: () => Resolvable<R>): Bluebird<R>;
707 static attempt<R>(fn: () => Resolvable<R>): Bluebird<R>;
708
709 /**
710 * Returns a new function that wraps the given function `fn`.
711 * The new function will always return a promise that is fulfilled with the original functions return values or rejected with thrown exceptions from the original function.
712 * This method is convenient when a function can sometimes return synchronously or throw synchronously.
713 */
714 static method<R>(fn: () => Resolvable<R>): () => Bluebird<R>;
715 static method<R, A1>(fn: (arg1: A1) => Resolvable<R>): (arg1: A1) => Bluebird<R>;
716 static method<R, A1, A2>(fn: (arg1: A1, arg2: A2) => Resolvable<R>): (arg1: A1, arg2: A2) => Bluebird<R>;
717 static method<R, A1, A2, A3>(
718 fn: (arg1: A1, arg2: A2, arg3: A3) => Resolvable<R>,
719 ): (arg1: A1, arg2: A2, arg3: A3) => Bluebird<R>;
720 static method<R, A1, A2, A3, A4>(
721 fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Resolvable<R>,
722 ): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Bluebird<R>;
723 static method<R, A1, A2, A3, A4, A5>(
724 fn: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Resolvable<R>,
725 ): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Bluebird<R>;
726 static method<R>(fn: (...args: any[]) => Resolvable<R>): (...args: any[]) => Bluebird<R>;
727
728 /**
729 * Create a promise that is resolved with the given `value`. If `value` is a thenable or promise, the returned promise will assume its state.
730 */
731 static resolve(): Bluebird<void>;
732 static resolve<R>(value: Resolvable<R>): Bluebird<R>;
733
734 /**
735 * Create a promise that is rejected with the given `reason`.
736 */
737 static reject(reason: any): Bluebird<never>;
738
739 /**
740 * @deprecated
741 * Create a promise with undecided fate and return a `PromiseResolver` to control it. See resolution?: Promise(#promise-resolution).
742 * @see http://bluebirdjs.com/docs/deprecated-apis.html#promise-resolution
743 */
744 static defer<R>(): Bluebird.Resolver<R>;
745
746 /**
747 * Cast the given `value` to a trusted promise.
748 *
749 * If `value` is already a trusted `Promise`, it is returned as is. If `value` is not a thenable, a fulfilled is: Promise returned with `value` as its fulfillment value.
750 * If `value` is a thenable (Promise-like object, like those returned by jQuery's `$.ajax`), returns a trusted that: Promise assimilates the state of the thenable.
751 */
752 static cast<R>(value: Resolvable<R>): Bluebird<R>;
753
754 /**
755 * Sugar for `Promise.resolve(undefined).bind(thisArg);`. See `.bind()`.
756 */
757 static bind(thisArg: any): Bluebird<void>;
758
759 /**
760 * See if `value` is a trusted Promise.
761 */
762 static is(value: any): boolean;
763
764 /**
765 * Call this right after the library is loaded to enabled long stack traces.
766 *
767 * Long stack traces cannot be disabled after being enabled, and cannot be enabled after promises have already been created.
768 * Long stack traces imply a substantial performance penalty, around 4-5x for throughput and 0.5x for latency.
769 */
770 static longStackTraces(): void;
771
772 /**
773 * Returns a promise that will be resolved with value (or undefined) after given ms milliseconds.
774 * If value is a promise, the delay will start counting down when it is fulfilled and the returned
775 * promise will be fulfilled with the fulfillment value of the value promise.
776 */
777 static delay<R>(ms: number, value: Resolvable<R>): Bluebird<R>;
778 static delay(ms: number): Bluebird<void>;
779
780 /**
781 * Returns a function that will wrap the given `nodeFunction`.
782 *
783 * Instead of taking a callback, the returned function will return a promise whose fate is decided by the callback behavior of the given node function.
784 * The node function should conform to node.js convention of accepting a callback as last argument and
785 * calling that callback with error as the first argument and success value on the second argument.
786 *
787 * If the `nodeFunction` calls its callback with multiple success values, the fulfillment value will be an array of them.
788 *
789 * If you pass a `receiver`, the `nodeFunction` will be called as a method on the `receiver`.
790 */
791 static promisify<T>(
792 func: (callback: (err: any, result?: T) => void) => void,
793 options?: Bluebird.PromisifyOptions,
794 ): () => Bluebird<T>;
795 static promisify<T, A1>(
796 func: (arg1: A1, callback: (err: any, result?: T) => void) => void,
797 options?: Bluebird.PromisifyOptions,
798 ): (arg1: A1) => Bluebird<T>;
799 static promisify<T, A1, A2>(
800 func: (arg1: A1, arg2: A2, callback: (err: any, result?: T) => void) => void,
801 options?: Bluebird.PromisifyOptions,
802 ): (arg1: A1, arg2: A2) => Bluebird<T>;
803 static promisify<T, A1, A2, A3>(
804 func: (arg1: A1, arg2: A2, arg3: A3, callback: (err: any, result?: T) => void) => void,
805 options?: Bluebird.PromisifyOptions,
806 ): (arg1: A1, arg2: A2, arg3: A3) => Bluebird<T>;
807 static promisify<T, A1, A2, A3, A4>(
808 func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, callback: (err: any, result?: T) => void) => void,
809 options?: Bluebird.PromisifyOptions,
810 ): (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Bluebird<T>;
811 static promisify<T, A1, A2, A3, A4, A5>(
812 func: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5, callback: (err: any, result?: T) => void) => void,
813 options?: Bluebird.PromisifyOptions,
814 ): (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Bluebird<T>;
815 static promisify(
816 nodeFunction: (...args: any[]) => void,
817 options?: Bluebird.PromisifyOptions,
818 ): (...args: any[]) => Bluebird<any>;
819
820 /**
821 * Promisifies the entire object by going through the object's properties and creating an async equivalent of each function on the object and its prototype chain.
822 *
823 * The promisified method name will be the original method name postfixed with `Async`. Returns the input object.
824 *
825 * Note that the original methods on the object are not overwritten but new methods are created with the `Async`-postfix. For example,
826 * if you `promisifyAll()` the node.js `fs` object use `fs.statAsync()` to call the promisified `stat` method.
827 */
828 // TODO how to model promisifyAll?
829 static promisifyAll<T extends object>(target: T, options?: Bluebird.PromisifyAllOptions<T>): PromisifyAll<T>;
830
831 /**
832 * Returns a promise that is resolved by a node style callback function.
833 */
834 static fromNode<T>(
835 resolver: (callback: (err: any, result?: T) => void) => void,
836 options?: Bluebird.FromNodeOptions,
837 ): Bluebird<T>;
838 static fromCallback<T>(
839 resolver: (callback: (err: any, result?: T) => void) => void,
840 options?: Bluebird.FromNodeOptions,
841 ): Bluebird<T>;
842
843 /**
844 * Returns a function that can use `yield` to run asynchronous code synchronously.
845 *
846 * This feature requires the support of generators which are drafted in the next version of the language.
847 * Node version greater than `0.11.2` is required and needs to be executed with the `--harmony-generators` (or `--harmony`) command-line switch.
848 */
849 // TODO: After https://github.com/Microsoft/TypeScript/issues/2983 is implemented, we can use
850 // the return type propagation of generators to automatically infer the return type T.
851 static coroutine<T>(
852 generatorFunction: () => IterableIterator<any>,
853 options?: Bluebird.CoroutineOptions,
854 ): () => Bluebird<T>;
855 static coroutine<T, A1>(
856 generatorFunction: (a1: A1) => IterableIterator<any>,
857 options?: Bluebird.CoroutineOptions,
858 ): (a1: A1) => Bluebird<T>;
859 static coroutine<T, A1, A2>(
860 generatorFunction: (a1: A1, a2: A2) => IterableIterator<any>,
861 options?: Bluebird.CoroutineOptions,
862 ): (a1: A1, a2: A2) => Bluebird<T>;
863 static coroutine<T, A1, A2, A3>(
864 generatorFunction: (a1: A1, a2: A2, a3: A3) => IterableIterator<any>,
865 options?: Bluebird.CoroutineOptions,
866 ): (a1: A1, a2: A2, a3: A3) => Bluebird<T>;
867 static coroutine<T, A1, A2, A3, A4>(
868 generatorFunction: (a1: A1, a2: A2, a3: A3, a4: A4) => IterableIterator<any>,
869 options?: Bluebird.CoroutineOptions,
870 ): (a1: A1, a2: A2, a3: A3, a4: A4) => Bluebird<T>;
871 static coroutine<T, A1, A2, A3, A4, A5>(
872 generatorFunction: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) => IterableIterator<any>,
873 options?: Bluebird.CoroutineOptions,
874 ): (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5) => Bluebird<T>;
875 static coroutine<T, A1, A2, A3, A4, A5, A6>(
876 generatorFunction: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6) => IterableIterator<any>,
877 options?: Bluebird.CoroutineOptions,
878 ): (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6) => Bluebird<T>;
879 static coroutine<T, A1, A2, A3, A4, A5, A6, A7>(
880 generatorFunction: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7) => IterableIterator<any>,
881 options?: Bluebird.CoroutineOptions,
882 ): (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7) => Bluebird<T>;
883 static coroutine<T, A1, A2, A3, A4, A5, A6, A7, A8>(
884 generatorFunction: (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8) => IterableIterator<any>,
885 options?: Bluebird.CoroutineOptions,
886 ): (a1: A1, a2: A2, a3: A3, a4: A4, a5: A5, a6: A6, a7: A7, a8: A8) => Bluebird<T>;
887
888 /**
889 * Add `handler` as the handler to call when there is a possibly unhandled rejection. The default handler logs the error stack to stderr or `console.error` in browsers.
890 *
891 * Passing no value or a non-function will have the effect of removing any kind of handling for possibly unhandled rejections.
892 */
893 static onPossiblyUnhandledRejection(handler: (reason: any) => any): void;
894
895 /**
896 * Add handler as the handler to call when there is a possibly unhandled rejection.
897 * The default handler logs the error stack to stderr or console.error in browsers.
898 *
899 * Passing no value or a non-function will have the effect of removing any kind of handling for possibly unhandled rejections.
900 *
901 * Note: this hook is specific to the bluebird instance its called on, application developers should use global rejection events.
902 */
903 static onPossiblyUnhandledRejection(handler?: (error: Error, promise: Bluebird<any>) => void): void;
904
905 /**
906 * Given an array, or a promise of an array, which contains promises (or a mix of promises and values) return a promise that is fulfilled when all the items in the array are fulfilled.
907 * The promise's fulfillment value is an array with fulfillment values at respective positions to the original array.
908 * If any promise in the array rejects, the returned promise is rejected with the rejection reason.
909 */
910 // TODO enable more overloads
911 // array with promises of different types
912 static all<T1, T2, T3, T4, T5>(
913 values: [Resolvable<T1>, Resolvable<T2>, Resolvable<T3>, Resolvable<T4>, Resolvable<T5>],
914 ): Bluebird<[T1, T2, T3, T4, T5]>;
915 static all<T1, T2, T3, T4>(
916 values: [Resolvable<T1>, Resolvable<T2>, Resolvable<T3>, Resolvable<T4>],
917 ): Bluebird<[T1, T2, T3, T4]>;
918 static all<T1, T2, T3>(values: [Resolvable<T1>, Resolvable<T2>, Resolvable<T3>]): Bluebird<[T1, T2, T3]>;
919 static all<T1, T2>(values: [Resolvable<T1>, Resolvable<T2>]): Bluebird<[T1, T2]>;
920 static all<T1>(values: [Resolvable<T1>]): Bluebird<[T1]>;
921 // array with values
922 static all<R>(values: Resolvable<Iterable<Resolvable<R>>>): Bluebird<R[]>;
923
924 static allSettled<T1, T2, T3, T4, T5>(
925 values: [Resolvable<T1>, Resolvable<T2>, Resolvable<T3>, Resolvable<T4>, Resolvable<T5>],
926 ): Bluebird<
927 [
928 Bluebird.Inspection<T1>,
929 Bluebird.Inspection<T2>,
930 Bluebird.Inspection<T3>,
931 Bluebird.Inspection<T4>,
932 Bluebird.Inspection<T5>,
933 ]
934 >;
935 static allSettled<T1, T2, T3, T4>(
936 values: [Resolvable<T1>, Resolvable<T2>, Resolvable<T3>, Resolvable<T4>],
937 ): Bluebird<[Bluebird.Inspection<T1>, Bluebird.Inspection<T2>, Bluebird.Inspection<T3>, Bluebird.Inspection<T4>]>;
938 static allSettled<T1, T2, T3>(
939 values: [Resolvable<T1>, Resolvable<T2>, Resolvable<T3>],
940 ): Bluebird<[Bluebird.Inspection<T1>, Bluebird.Inspection<T2>, Bluebird.Inspection<T3>]>;
941 static allSettled<T1, T2>(
942 values: [Resolvable<T1>, Resolvable<T2>],
943 ): Bluebird<[Bluebird.Inspection<T1>, Bluebird.Inspection<T2>]>;
944 static allSettled<T1>(values: [Resolvable<T1>]): Bluebird<[Bluebird.Inspection<T1>]>;
945 static allSettled<R>(values: Resolvable<Iterable<Resolvable<R>>>): Bluebird<Array<Bluebird.Inspection<R>>>;
946
947 /**
948 * Like ``Promise.all`` but for object properties instead of array items. Returns a promise that is fulfilled when all the properties of the object are fulfilled.
949 *
950 * The promise's fulfillment value is an object with fulfillment values at respective keys to the original object.
951 * If any promise in the object rejects, the returned promise is rejected with the rejection reason.
952 *
953 * If `object` is a trusted `Promise`, then it will be treated as a promise for object rather than for its properties.
954 * All other objects are treated for their properties as is returned by `Object.keys` - the object's own enumerable properties.
955 *
956 * *The original object is not modified.*
957 */
958 // map
959 static props<K, V>(map: Resolvable<Map<K, Resolvable<V>>>): Bluebird<Map<K, V>>;
960 // trusted promise for object
961 static props<T>(object: PromiseLike<Bluebird.ResolvableProps<T>>): Bluebird<T>;
962 // object
963 static props<T>(object: Bluebird.ResolvableProps<T>): Bluebird<T>; // tslint:disable-line:unified-signatures
964
965 /**
966 * Like `Promise.some()`, with 1 as `count`. However, if the promise fulfills, the fulfillment value is not an array of 1 but the value directly.
967 */
968 static any<R>(values: Resolvable<Iterable<Resolvable<R>>>): Bluebird<R>;
969
970 /**
971 * Given an array, or a promise of an array, which contains promises (or a mix of promises and values) return a promise that is
972 * fulfilled or rejected as soon as a promise in the array is fulfilled or rejected with the respective rejection reason or fulfillment value.
973 *
974 * **Note** If you pass empty array or a sparse array with no values, or a promise/thenable for such, it will be forever pending.
975 */
976 static race<R>(values: Resolvable<Iterable<Resolvable<R>>>): Bluebird<R>;
977
978 /**
979 * Initiate a competitive race between multiple promises or values (values will become immediately fulfilled promises).
980 * When `count` amount of promises have been fulfilled, the returned promise is fulfilled with an array that contains the fulfillment values of
981 * the winners in order of resolution.
982 *
983 * If too many promises are rejected so that the promise can never become fulfilled,
984 * it will be immediately rejected with an array of rejection reasons in the order they were thrown in.
985 *
986 * *The original array is not modified.*
987 */
988 static some<R>(values: Resolvable<Iterable<Resolvable<R>>>, count: number): Bluebird<R[]>;
989
990 /**
991 * Promise.join(
992 * Promise<any>|any values...,
993 * function handler
994 * ) -> Promise
995 * For coordinating multiple concurrent discrete promises.
996 *
997 * Note: In 1.x and 0.x Promise.join used to be a Promise.all that took the values in as arguments instead in an array.
998 * This behavior has been deprecated but is still supported partially - when the last argument is an immediate function value the new semantics will apply
999 */
1000 static join<R, A1>(
1001 arg1: Resolvable<A1>,
1002 handler: (arg1: A1) => Resolvable<R>,
1003 ): Bluebird<R>;
1004 static join<R, A1, A2>(
1005 arg1: Resolvable<A1>,
1006 arg2: Resolvable<A2>,
1007 handler: (arg1: A1, arg2: A2) => Resolvable<R>,
1008 ): Bluebird<R>;
1009 static join<R, A1, A2, A3>(
1010 arg1: Resolvable<A1>,
1011 arg2: Resolvable<A2>,
1012 arg3: Resolvable<A3>,
1013 handler: (arg1: A1, arg2: A2, arg3: A3) => Resolvable<R>,
1014 ): Bluebird<R>;
1015 static join<R, A1, A2, A3, A4>(
1016 arg1: Resolvable<A1>,
1017 arg2: Resolvable<A2>,
1018 arg3: Resolvable<A3>,
1019 arg4: Resolvable<A4>,
1020 handler: (arg1: A1, arg2: A2, arg3: A3, arg4: A4) => Resolvable<R>,
1021 ): Bluebird<R>;
1022 static join<R, A1, A2, A3, A4, A5>(
1023 arg1: Resolvable<A1>,
1024 arg2: Resolvable<A2>,
1025 arg3: Resolvable<A3>,
1026 arg4: Resolvable<A4>,
1027 arg5: Resolvable<A5>,
1028 handler: (arg1: A1, arg2: A2, arg3: A3, arg4: A4, arg5: A5) => Resolvable<R>,
1029 ): Bluebird<R>;
1030
1031 // variadic array
1032 /** @deprecated use .all instead */
1033 static join<R>(...values: Array<Resolvable<R>>): Bluebird<R[]>;
1034
1035 /**
1036 * Map an array, or a promise of an array,
1037 * which contains a promises (or a mix of promises and values) with the given `mapper` function with the signature `(item, index, arrayLength)`
1038 * where `item` is the resolved value of a respective promise in the input array.
1039 * If any promise in the input array is rejected the returned promise is rejected as well.
1040 *
1041 * If the `mapper` function returns promises or thenables, the returned promise will wait for all the mapped results to be resolved as well.
1042 *
1043 * *The original array is not modified.*
1044 */
1045 static map<R, U>(
1046 values: Resolvable<Iterable<Resolvable<R>>>,
1047 mapper: IterateFunction<R, U>,
1048 options?: Bluebird.ConcurrencyOption,
1049 ): Bluebird<U[]>;
1050
1051 /**
1052 * Reduce an array, or a promise of an array,
1053 * which contains a promises (or a mix of promises and values) with the given `reducer` function with the signature `(total, current, index, arrayLength)`
1054 * where `item` is the resolved value of a respective promise in the input array.
1055 * If any promise in the input array is rejected the returned promise is rejected as well.
1056 *
1057 * If the reducer function returns a promise or a thenable, the result for the promise is awaited for before continuing with next iteration.
1058 *
1059 * *The original array is not modified. If no `initialValue` is given and the array doesn't contain at least 2 items,
1060 * the callback will not be called and `undefined` is returned.
1061 *
1062 * If `initialValue` is given and the array doesn't have at least 1 item, `initialValue` is returned.*
1063 */
1064 static reduce<R, U>(
1065 values: Resolvable<Iterable<Resolvable<R>>>,
1066 reducer: (total: U, current: R, index: number, arrayLength: number) => Resolvable<U>,
1067 initialValue?: Resolvable<U>,
1068 ): Bluebird<U>;
1069
1070 /**
1071 * Filter an array, or a promise of an array,
1072 * which contains a promises (or a mix of promises and values) with the given `filterer` function with the signature `(item, index, arrayLength)`
1073 * where `item` is the resolved value of a respective promise in the input array.
1074 * If any promise in the input array is rejected the returned promise is rejected as well.
1075 *
1076 * The return values from the filtered functions are coerced to booleans, with the exception of promises and thenables which are awaited for their eventual result.
1077 *
1078 * *The original array is not modified.
1079 */
1080 static filter<R>(
1081 values: Resolvable<Iterable<Resolvable<R>>>,
1082 filterer: IterateFunction<R, boolean>,
1083 option?: Bluebird.ConcurrencyOption,
1084 ): Bluebird<R[]>;
1085
1086 /**
1087 * Iterate over an array, or a promise of an array,
1088 * which contains promises (or a mix of promises and values) with the given iterator function with the signature `(item, index, value)`
1089 * where item is the resolved value of a respective promise in the input array.
1090 * Iteration happens serially. If any promise in the input array is rejected the returned promise is rejected as well.
1091 *
1092 * Resolves to the original array unmodified, this method is meant to be used for side effects.
1093 * If the iterator function returns a promise or a thenable, the result for the promise is awaited for before continuing with next iteration.
1094 */
1095 static each<R>(
1096 values: Resolvable<Iterable<Resolvable<R>>>,
1097 iterator: IterateFunction<R, any>,
1098 ): Bluebird<R[]>;
1099
1100 /**
1101 * Given an Iterable(arrays are Iterable), or a promise of an Iterable, which produces promises (or a mix of promises and values),
1102 * iterate over all the values in the Iterable into an array and iterate over the array serially, in-order.
1103 *
1104 * Returns a promise for an array that contains the values returned by the iterator function in their respective positions.
1105 * The iterator won't be called for an item until its previous item, and the promise returned by the iterator for that item are fulfilled.
1106 * This results in a mapSeries kind of utility but it can also be used simply as a side effect iterator similar to Array#forEach.
1107 *
1108 * If any promise in the input array is rejected or any promise returned by the iterator function is rejected, the result will be rejected as well.
1109 */
1110 static mapSeries<R, U>(
1111 values: Resolvable<Iterable<Resolvable<R>>>,
1112 iterator: IterateFunction<R, U>,
1113 ): Bluebird<U[]>;
1114
1115 /**
1116 * A meta method used to specify the disposer method that cleans up a resource when using `Promise.using`.
1117 *
1118 * Returns a Disposer object which encapsulates both the resource as well as the method to clean it up.
1119 * The user can pass this object to `Promise.using` to get access to the resource when it becomes available,
1120 * as well as to ensure its automatically cleaned up.
1121 *
1122 * The second argument passed to a disposer is the result promise of the using block, which you can
1123 * inspect synchronously.
1124 */
1125 disposer(disposeFn: (arg: R, promise: Bluebird<R>) => Resolvable<void>): Bluebird.Disposer<R>;
1126
1127 /**
1128 * In conjunction with `.disposer`, using will make sure that no matter what, the specified disposer
1129 * will be called when the promise returned by the callback passed to using has settled. The disposer is
1130 * necessary because there is no standard interface in node for disposing resources.
1131 */
1132 static using<R, T>(
1133 disposer: Bluebird.Disposer<R>,
1134 executor: (transaction: R) => PromiseLike<T>,
1135 ): Bluebird<T>;
1136 static using<R1, R2, T>(
1137 disposer: Bluebird.Disposer<R1>,
1138 disposer2: Bluebird.Disposer<R2>,
1139 executor: (transaction1: R1, transaction2: R2) => PromiseLike<T>,
1140 ): Bluebird<T>;
1141 static using<R1, R2, R3, T>(
1142 disposer: Bluebird.Disposer<R1>,
1143 disposer2: Bluebird.Disposer<R2>,
1144 disposer3: Bluebird.Disposer<R3>,
1145 executor: (transaction1: R1, transaction2: R2, transaction3: R3) => PromiseLike<T>,
1146 ): Bluebird<T>;
1147
1148 /**
1149 * Configure long stack traces, warnings, monitoring and cancellation.
1150 * Note that even though false is the default here, a development environment might be detected which automatically
1151 * enables long stack traces and warnings.
1152 */
1153 static config(options: {
1154 /** Enable warnings */
1155 warnings?: boolean | {
1156 /** Enables all warnings except forgotten return statements. */
1157 wForgottenReturn: boolean;
1158 } | undefined;
1159 /** Enable long stack traces */
1160 longStackTraces?: boolean | undefined;
1161 /** Enable cancellation */
1162 cancellation?: boolean | undefined;
1163 /** Enable monitoring */
1164 monitoring?: boolean | undefined;
1165 /** Enable async hooks */
1166 asyncHooks?: boolean | undefined;
1167 }): void;
1168
1169 /**
1170 * Create a new promise. The passed in function will receive functions `resolve` and `reject` as its arguments which can be called to seal the fate of the created promise.
1171 * If promise cancellation is enabled, passed in function will receive one more function argument `onCancel` that allows to register an optional cancellation callback.
1172 */
1173 static Promise: typeof Bluebird;
1174
1175 /**
1176 * The version number of the library
1177 */
1178 static version: string;
1179}
1180
1181declare namespace Bluebird {
1182 interface ConcurrencyOption {
1183 concurrency: number;
1184 }
1185 interface SpreadOption {
1186 spread: boolean;
1187 }
1188 interface FromNodeOptions {
1189 multiArgs?: boolean | undefined;
1190 }
1191 interface PromisifyOptions {
1192 context?: any;
1193 multiArgs?: boolean | undefined;
1194 }
1195 interface PromisifyAllOptions<T> extends PromisifyOptions {
1196 suffix?: string | undefined;
1197 filter?(name: string, func: (...args: any[]) => any, target?: any, passesDefaultFilter?: boolean): boolean;
1198 // The promisifier gets a reference to the original method and should return a function which returns a promise
1199 promisifier?(
1200 this: T,
1201 originalMethod: (...args: any[]) => any,
1202 defaultPromisifer: (...args: any[]) => (...args: any[]) => Bluebird<any>,
1203 ): () => PromiseLike<any>;
1204 }
1205 interface CoroutineOptions {
1206 yieldHandler(value: any): any;
1207 }
1208
1209 /**
1210 * Represents an error is an explicit promise rejection as opposed to a thrown error.
1211 * For example, if an error is errbacked by a callback API promisified through undefined or undefined
1212 * and is not a typed error, it will be converted to a `OperationalError` which has the original error in
1213 * the `.cause` property.
1214 *
1215 * `OperationalError`s are caught in `.error` handlers.
1216 */
1217 class OperationalError extends Error {}
1218
1219 /**
1220 * Signals that an operation has timed out. Used as a custom cancellation reason in `.timeout`.
1221 */
1222 class TimeoutError extends Error {}
1223
1224 /**
1225 * Signals that an operation has been aborted or cancelled. The default reason used by `.cancel`.
1226 */
1227 class CancellationError extends Error {}
1228
1229 /**
1230 * A collection of errors. `AggregateError` is an array-like object, with numeric indices and a `.length` property.
1231 * It supports all generic array methods such as `.forEach` directly.
1232 *
1233 * `AggregateError`s are caught in `.error` handlers, even if the contained errors are not operational.
1234 *
1235 * `Promise.some` and `Promise.any` use `AggregateError` as rejection reason when they fail.
1236 */
1237 class AggregateError extends Error implements ArrayLike<Error> {
1238 length: number;
1239 [index: number]: Error;
1240 join(separator?: string): string;
1241 pop(): Error;
1242 push(...errors: Error[]): number;
1243 shift(): Error;
1244 unshift(...errors: Error[]): number;
1245 slice(begin?: number, end?: number): AggregateError;
1246 filter(
1247 callback: (element: Error, index: number, array: AggregateError) => boolean,
1248 thisArg?: any,
1249 ): AggregateError;
1250 forEach(callback: (element: Error, index: number, array: AggregateError) => void, thisArg?: any): undefined;
1251 some(callback: (element: Error, index: number, array: AggregateError) => boolean, thisArg?: any): boolean;
1252 every(callback: (element: Error, index: number, array: AggregateError) => boolean, thisArg?: any): boolean;
1253 map(callback: (element: Error, index: number, array: AggregateError) => boolean, thisArg?: any): AggregateError;
1254 indexOf(searchElement: Error, fromIndex?: number): number;
1255 lastIndexOf(searchElement: Error, fromIndex?: number): number;
1256 reduce(
1257 callback: (accumulator: any, element: Error, index: number, array: AggregateError) => any,
1258 initialValue?: any,
1259 ): any;
1260 reduceRight(
1261 callback: (previousValue: any, element: Error, index: number, array: AggregateError) => any,
1262 initialValue?: any,
1263 ): any;
1264 sort(compareFunction?: (errLeft: Error, errRight: Error) => number): AggregateError;
1265 reverse(): AggregateError;
1266 }
1267
1268 /**
1269 * returned by `Bluebird.disposer()`.
1270 */
1271 class Disposer<R> {}
1272
1273 /** @deprecated Use PromiseLike<T> directly. */
1274 type Thenable<T> = PromiseLike<T>;
1275
1276 type ResolvableProps<T> = object & { [K in keyof T]: Resolvable<T[K]> };
1277
1278 interface Resolver<R> {
1279 /**
1280 * Returns a reference to the controlled promise that can be passed to clients.
1281 */
1282 promise: Bluebird<R>;
1283
1284 /**
1285 * Resolve the underlying promise with `value` as the resolution value. If `value` is a thenable or a promise, the underlying promise will assume its state.
1286 */
1287 resolve(value: R): void;
1288 resolve(): void;
1289
1290 /**
1291 * Reject the underlying promise with `reason` as the rejection reason.
1292 */
1293 reject(reason: any): void;
1294
1295 /**
1296 * Gives you a callback representation of the `PromiseResolver`. Note that this is not a method but a property.
1297 * The callback accepts error object in first argument and success values on the 2nd parameter and the rest, I.E. node js conventions.
1298 *
1299 * If the the callback is called with multiple success values, the resolver fulfills its promise with an array of the values.
1300 */
1301 // TODO specify resolver callback
1302 callback(err: any, value: R, ...values: R[]): void;
1303 }
1304
1305 interface Inspection<R> {
1306 /**
1307 * See if the underlying promise was fulfilled at the creation time of this inspection object.
1308 */
1309 isFulfilled(): boolean;
1310
1311 /**
1312 * See if the underlying promise was rejected at the creation time of this inspection object.
1313 */
1314 isRejected(): boolean;
1315
1316 /**
1317 * See if the underlying promise was cancelled at the creation time of this inspection object.
1318 */
1319 isCancelled(): boolean;
1320
1321 /**
1322 * See if the underlying promise was defer at the creation time of this inspection object.
1323 */
1324 isPending(): boolean;
1325
1326 /**
1327 * Get the fulfillment value of the underlying promise. Throws if the promise wasn't fulfilled at the creation time of this inspection object.
1328 *
1329 * throws `TypeError`
1330 */
1331 value(): R;
1332
1333 /**
1334 * Get the rejection reason for the underlying promise. Throws if the promise wasn't rejected at the creation time of this inspection object.
1335 *
1336 * throws `TypeError`
1337 */
1338 reason(): any;
1339 }
1340
1341 /**
1342 * Returns a new independent copy of the Bluebird library.
1343 *
1344 * This method should be used before you use any of the methods which would otherwise alter the global Bluebird object - to avoid polluting global state.
1345 */
1346 function getNewLibraryCopy(): typeof Bluebird;
1347
1348 /**
1349 * This is relevant to browser environments with no module loader.
1350 *
1351 * Release control of the Promise namespace to whatever it was before this library was loaded.
1352 * Returns a reference to the library namespace so you can attach it to something else.
1353 */
1354 function noConflict(): typeof Bluebird;
1355
1356 /**
1357 * Changes how bluebird schedules calls a-synchronously.
1358 *
1359 * @param scheduler Should be a function that asynchronously schedules
1360 * the calling of the passed in function
1361 */
1362 function setScheduler(scheduler: (callback: (...args: any[]) => void) => void): void;
1363}
1364
1365export = Bluebird;