UNPKG

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