UNPKG

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