UNPKG

38.9 kBTypeScriptView Raw
1// Type definitions for bluebird 1.0.0
2// Project: https://github.com/petkaantonov/bluebird
3// Definitions by: Bart van der Schoor <https://github.com/Bartvds>
4// Definitions: https://github.com/borisyankov/DefinitelyTyped
5
6// ES6 model with generics overload was sourced and trans-multiplied from es6-promises.d.ts
7// By: Campredon <https://github.com/fdecampredon/>
8
9// Warning: recommended to use `tsc > v0.9.7` (critical bugs in earlier generic code):
10// - https://github.com/borisyankov/DefinitelyTyped/issues/1563
11
12// Note: replicate changes to all overloads in both definition and test file
13// Note: keep both static and instance members inline (so similar)
14
15// TODO fix remaining TODO annotations in both definition and test
16
17// TODO verify support to have no return statement in handlers to get a Promise<void> (more overloads?)
18
19declare class Promise<R> implements Promise.Thenable<R>, Promise.Inspection<R> {
20 /**
21 * 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.
22 */
23 constructor(callback: (resolve: (thenable: Promise.Thenable<R>) => void, reject: (error: any) => void) => void);
24 constructor(callback: (resolve: (result: R) => void, reject: (error: any) => void) => void);
25
26 /**
27 * Promises/A+ `.then()` with progress handler. Returns a new promise chained from this promise. The new promise will be rejected or resolved dedefer on the passed `fulfilledHandler`, `rejectedHandler` and the state of this promise.
28 */
29 then<U>(onFulfill: (value: R) => Promise.Thenable<U>, onReject: (error: any) => Promise.Thenable<U>, onProgress?: (note: any) => any): Promise<U>;
30 then<U>(onFulfill: (value: R) => Promise.Thenable<U>, onReject?: (error: any) => U, onProgress?: (note: any) => any): Promise<U>;
31 then<U>(onFulfill: (value: R) => U, onReject: (error: any) => Promise.Thenable<U>, onProgress?: (note: any) => any): Promise<U>;
32 then<U>(onFulfill?: (value: R) => U, onReject?: (error: any) => U, onProgress?: (note: any) => any): Promise<U>;
33
34 /**
35 * This is a catch-all exception handler, shortcut for calling `.then(null, handler)` on this promise. Any exception happening in a `.then`-chain will propagate to nearest `.catch` handler.
36 *
37 * Alias `.caught();` for compatibility with earlier ECMAScript version.
38 */
39 catch<U>(onReject?: (error: any) => Promise.Thenable<U>): Promise<U>;
40 caught<U>(onReject?: (error: any) => Promise.Thenable<U>): Promise<U>;
41
42 catch<U>(onReject?: (error: any) => U): Promise<U>;
43 caught<U>(onReject?: (error: any) => U): Promise<U>;
44
45 /**
46 * This extends `.catch` to work more like catch-clauses in languages like Java or C#. Instead of manually checking `instanceof` or `.name === "SomeError"`, you may specify a number of error constructors which are eligible for this catch handler. The catch handler that is first met that has eligible constructors specified, is the one that will be called.
47 *
48 * This method also supports predicate-based filters. If you pass a predicate function instead of an error constructor, the predicate will receive the error as an argument. The return result of the predicate will be used determine whether the error handler should be called.
49 *
50 * Alias `.caught();` for compatibility with earlier ECMAScript version.
51 */
52 catch<U>(predicate: (error: any) => boolean, onReject: (error: any) => Promise.Thenable<U>): Promise<U>;
53 caught<U>(predicate: (error: any) => boolean, onReject: (error: any) => Promise.Thenable<U>): Promise<U>;
54
55 catch<U>(predicate: (error: any) => boolean, onReject: (error: any) => U): Promise<U>;
56 caught<U>(predicate: (error: any) => boolean, onReject: (error: any) => U): Promise<U>;
57
58 catch<U>(ErrorClass: Function, onReject: (error: any) => Promise.Thenable<U>): Promise<U>;
59 caught<U>(ErrorClass: Function, onReject: (error: any) => Promise.Thenable<U>): Promise<U>;
60
61 catch<U>(ErrorClass: Function, onReject: (error: any) => U): Promise<U>;
62 caught<U>(ErrorClass: Function, onReject: (error: any) => U): Promise<U>;
63
64 /**
65 * Like `.catch` but instead of catching all types of exceptions, it only catches those that don't originate from thrown errors but rather from explicit rejections.
66 */
67 error<U>(onReject: (reason: any) => Promise.Thenable<U>): Promise<U>;
68 error<U>(onReject: (reason: any) => U): Promise<U>;
69
70 /**
71 * Pass a handler that will be called regardless of this promise's fate. Returns a new promise chained from this promise. There are special semantics for `.finally()` in that the final value cannot be modified from the handler.
72 *
73 * Alias `.lastly();` for compatibility with earlier ECMAScript version.
74 */
75 finally<U>(handler: () => Promise.Thenable<U>): Promise<R>;
76 finally<U>(handler: () => U): Promise<R>;
77
78 lastly<U>(handler: () => Promise.Thenable<U>): Promise<R>;
79 lastly<U>(handler: () => U): Promise<R>;
80
81 /**
82 * Create a promise that follows this promise, but is bound to the given `thisArg` value. A bound promise will call its handlers with the bound value set to `this`. Additionally promises derived from a bound promise will also be bound promises with the same `thisArg` binding as the original promise.
83 */
84 bind(thisArg: any): Promise<R>;
85
86 /**
87 * Like `.then()`, but any unhandled rejection that ends up here will be thrown as an error.
88 */
89 done<U>(onFulfilled: (value: R) => Promise.Thenable<U>, onRejected: (error: any) => Promise.Thenable<U>, onProgress?: (note: any) => any): void;
90 done<U>(onFulfilled: (value: R) => Promise.Thenable<U>, onRejected?: (error: any) => U, onProgress?: (note: any) => any): void;
91 done<U>(onFulfilled: (value: R) => U, onRejected: (error: any) => Promise.Thenable<U>, onProgress?: (note: any) => any): void;
92 done<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => U, onProgress?: (note: any) => any): void;
93
94 /**
95 * Like `.finally()`, but not called for rejections.
96 */
97 tap<U>(onFulFill: (value: R) => Promise.Thenable<U>): Promise<R>;
98 tap<U>(onFulfill: (value: R) => U): Promise<R>;
99
100 /**
101 * Shorthand for `.then(null, null, handler);`. Attach a progress handler that will be called if this promise is progressed. Returns a new promise chained from this promise.
102 */
103 progressed(handler: (note: any) => any): Promise<R>;
104
105 /**
106 * Same as calling `Promise.delay(this, ms)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
107 */
108 delay(ms: number): Promise<R>;
109
110 /**
111 * Returns a promise that will be fulfilled with this promise's fulfillment value or rejection reason. However, if this promise is not fulfilled or rejected within `ms` milliseconds, the returned promise is rejected with a `Promise.TimeoutError` instance.
112 *
113 * You may specify a custom error message with the `message` parameter.
114 */
115 timeout(ms: number, message?: string): Promise<R>;
116
117 /**
118 * Register a node-style callback on this promise. When this promise is is either fulfilled or rejected, the node callback will be called back with the node.js convention where error reason is the first argument and success value is the second argument. The error argument will be `null` in case of success.
119 * Returns back this promise instead of creating a new one. If the `callback` argument is not a function, this method does not do anything.
120 */
121 nodeify(callback: (err: any, value?: R) => void): Promise<R>;
122 nodeify(...sink: any[]): void;
123
124 /**
125 * Marks this promise as cancellable. Promises by default are not cancellable after v0.11 and must be marked as such for `.cancel()` to have any effect. Marking a promise as cancellable is infectious and you don't need to remark any descendant promise.
126 */
127 cancellable(): Promise<R>;
128
129 /**
130 * Cancel this promise. The cancellation will propagate to farthest cancellable ancestor promise which is still pending.
131 *
132 * That ancestor will then be rejected with a `CancellationError` (get a reference from `Promise.CancellationError`) object as the rejection reason.
133 *
134 * In a promise rejection handler you may check for a cancellation by seeing if the reason object has `.name === "Cancel"`.
135 *
136 * Promises are by default not cancellable. Use `.cancellable()` to mark a promise as cancellable.
137 */
138 // TODO what to do with this?
139 cancel<U>(): Promise<U>;
140
141 /**
142 * Like `.then()`, but cancellation of the the returned promise or any of its descendant will not propagate cancellation to this promise or this promise's ancestors.
143 */
144 fork<U>(onFulfilled: (value: R) => Promise.Thenable<U>, onRejected: (error: any) => Promise.Thenable<U>, onProgress?: (note: any) => any): Promise<U>;
145 fork<U>(onFulfilled: (value: R) => Promise.Thenable<U>, onRejected?: (error: any) => U, onProgress?: (note: any) => any): Promise<U>;
146 fork<U>(onFulfilled: (value: R) => U, onRejected: (error: any) => Promise.Thenable<U>, onProgress?: (note: any) => any): Promise<U>;
147 fork<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => U, onProgress?: (note: any) => any): Promise<U>;
148
149 /**
150 * Create an uncancellable promise based on this promise.
151 */
152 uncancellable(): Promise<R>;
153
154 /**
155 * See if this promise can be cancelled.
156 */
157 isCancellable(): boolean;
158
159 /**
160 * See if this `promise` has been fulfilled.
161 */
162 isFulfilled(): boolean;
163
164 /**
165 * See if this `promise` has been rejected.
166 */
167 isRejected(): boolean;
168
169 /**
170 * See if this `promise` is still defer.
171 */
172 isPending(): boolean;
173
174 /**
175 * See if this `promise` is resolved -> either fulfilled or rejected.
176 */
177 isResolved(): boolean;
178
179 /**
180 * Get the fulfillment value of the underlying promise. Throws if the promise isn't fulfilled yet.
181 *
182 * throws `TypeError`
183 */
184 value(): R;
185
186 /**
187 * Get the rejection reason for the underlying promise. Throws if the promise isn't rejected yet.
188 *
189 * throws `TypeError`
190 */
191 reason(): any;
192
193 /**
194 * Synchronously inspect the state of this `promise`. The `PromiseInspection` will represent the state of the promise as snapshotted at the time of calling `.inspect()`.
195 */
196 inspect(): Promise.Inspection<R>;
197
198 /**
199 * This is a convenience method for doing:
200 *
201 * <code>
202 * promise.then(function(obj){
203 * return obj[propertyName].call(obj, arg...);
204 * });
205 * </code>
206 */
207 call(propertyName: string, ...args: any[]): Promise<any>;
208
209 /**
210 * This is a convenience method for doing:
211 *
212 * <code>
213 * promise.then(function(obj){
214 * return obj[propertyName];
215 * });
216 * </code>
217 */
218 // TODO find way to fix get()
219 // get<U>(propertyName: string): Promise<U>;
220
221 /**
222 * Convenience method for:
223 *
224 * <code>
225 * .then(function() {
226 * return value;
227 * });
228 * </code>
229 *
230 * in the case where `value` doesn't change its value. That means `value` is bound at the time of calling `.return()`
231 *
232 * Alias `.thenReturn();` for compatibility with earlier ECMAScript version.
233 */
234 return(): Promise<any>;
235 thenReturn(): Promise<any>;
236 return<U>(value: U): Promise<U>;
237 thenReturn<U>(value: U): Promise<U>;
238
239 /**
240 * Convenience method for:
241 *
242 * <code>
243 * .then(function() {
244 * throw reason;
245 * });
246 * </code>
247 * Same limitations apply as with `.return()`.
248 *
249 * Alias `.thenThrow();` for compatibility with earlier ECMAScript version.
250 */
251 throw(reason: Error): Promise<R>;
252 thenThrow(reason: Error): Promise<R>;
253
254 /**
255 * Convert to String.
256 */
257 toString(): string;
258
259 /**
260 * This is implicitly called by `JSON.stringify` when serializing the object. Returns a serialized representation of the `Promise`.
261 */
262 toJSON(): Object;
263
264 /**
265 * 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.
266 */
267 // TODO how to model instance.spread()? like Q?
268 spread<U>(onFulfill: Function, onReject?: (reason: any) => Promise.Thenable<U>): Promise<U>;
269 spread<U>(onFulfill: Function, onReject?: (reason: any) => U): Promise<U>;
270 /*
271 // TODO or something like this?
272 spread<U, W>(onFulfill: (...values: W[]) => Promise.Thenable<U>, onReject?: (reason: any) => Promise.Thenable<U>): Promise<U>;
273 spread<U, W>(onFulfill: (...values: W[]) => Promise.Thenable<U>, onReject?: (reason: any) => U): Promise<U>;
274 spread<U, W>(onFulfill: (...values: W[]) => U, onReject?: (reason: any) => Promise.Thenable<U>): Promise<U>;
275 spread<U, W>(onFulfill: (...values: W[]) => U, onReject?: (reason: any) => U): Promise<U>;
276 */
277 /**
278 * 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.
279 */
280 // TODO type inference from array-resolving promise?
281 all<U>(): Promise<U[]>;
282
283 /**
284 * 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.
285 */
286 // TODO how to model instance.props()?
287 props(): Promise<Object>;
288
289 /**
290 * Same as calling `Promise.settle(thisPromise)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
291 */
292 // TODO type inference from array-resolving promise?
293 settle<U>(): Promise<Promise.Inspection<U>[]>;
294
295 /**
296 * 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.
297 */
298 // TODO type inference from array-resolving promise?
299 any<U>(): Promise<U>;
300
301 /**
302 * 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.
303 */
304 // TODO type inference from array-resolving promise?
305 some<U>(count: number): Promise<U[]>;
306
307 /**
308 * 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.
309 */
310 // TODO type inference from array-resolving promise?
311 race<U>(): Promise<U>;
312
313 /**
314 * Same as calling `Promise.map(thisPromise, mapper)`. With the exception that if this promise is bound to a value, the returned promise is bound to that value too.
315 */
316 // TODO type inference from array-resolving promise?
317 map<Q, U>(mapper: (item: Q, index: number, arrayLength: number) => Promise.Thenable<U>): Promise<U[]>;
318 map<Q, U>(mapper: (item: Q, index: number, arrayLength: number) => U): Promise<U[]>;
319
320 /**
321 * 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.
322 */
323 // TODO type inference from array-resolving promise?
324 reduce<Q, U>(reducer: (memo: U, item: Q, index: number, arrayLength: number) => Promise.Thenable<U>, initialValue?: U): Promise<U>;
325 reduce<Q, U>(reducer: (memo: U, item: Q, index: number, arrayLength: number) => U, initialValue?: U): Promise<U>;
326
327 /**
328 * 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.
329 */
330 // TODO type inference from array-resolving promise?
331 filter<U>(filterer: (item: U, index: number, arrayLength: number) => Promise.Thenable<boolean>): Promise<U[]>;
332 filter<U>(filterer: (item: U, index: number, arrayLength: number) => boolean): Promise<U[]>;
333
334 /**
335 * Start the chain of promises with `Promise.try`. Any synchronous exceptions will be turned into rejections on the returned promise.
336 *
337 * Note about second argument: if it's specifically a true array, its values become respective arguments for the function call. Otherwise it is passed as is as the first argument for the function call.
338 *
339 * Alias for `attempt();` for compatibility with earlier ECMAScript version.
340 */
341 static try<R>(fn: () => Promise.Thenable<R>, args?: any[], ctx?: any): Promise<R>;
342 static try<R>(fn: () => R, args?: any[], ctx?: any): Promise<R>;
343
344 static attempt<R>(fn: () => Promise.Thenable<R>, args?: any[], ctx?: any): Promise<R>;
345 static attempt<R>(fn: () => R, args?: any[], ctx?: any): Promise<R>;
346
347 /**
348 * Returns a new function that wraps the given function `fn`. 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.
349 * This method is convenient when a function can sometimes return synchronously or throw synchronously.
350 */
351 static method(fn: Function): Function;
352
353 /**
354 * Create a promise that is resolved with the given `value`. If `value` is a thenable or promise, the returned promise will assume its state.
355 */
356 static resolve(): Promise<void>;
357 static resolve<R>(value: Promise.Thenable<R>): Promise<R>;
358 static resolve<R>(value: R): Promise<R>;
359
360 /**
361 * Create a promise that is rejected with the given `reason`.
362 */
363 static reject(reason: any): Promise<any>;
364 static reject<R>(reason: any): Promise<R>;
365
366 /**
367 * Create a promise with undecided fate and return a `PromiseResolver` to control it. See resolution?: Promise(#promise-resolution).
368 */
369 static defer<R>(): Promise.Resolver<R>;
370
371 /**
372 * Cast the given `value` to a trusted promise. 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. 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.
373 */
374 static cast<R>(value: Promise.Thenable<R>): Promise<R>;
375 static cast<R>(value: R): Promise<R>;
376
377 /**
378 * Sugar for `Promise.resolve(undefined).bind(thisArg);`. See `.bind()`.
379 */
380 static bind(thisArg: any): Promise<void>;
381
382 /**
383 * See if `value` is a trusted Promise.
384 */
385 static is(value: any): boolean;
386
387 /**
388 * Call this right after the library is loaded to enabled long stack traces. Long stack traces cannot be disabled after being enabled, and cannot be enabled after promises have alread been created. Long stack traces imply a substantial performance penalty, around 4-5x for throughput and 0.5x for latency.
389 */
390 static longStackTraces(): void;
391
392 /**
393 * Returns a promise that will be fulfilled with `value` (or `undefined`) after given `ms` milliseconds. If `value` is a promise, the delay will start counting down when it is fulfilled and the returned promise will be fulfilled with the fulfillment value of the `value` promise.
394 */
395 // TODO enable more overloads
396 static delay<R>(value: Promise.Thenable<R>, ms: number): Promise<R>;
397 static delay<R>(value: R, ms: number): Promise<R>;
398 static delay(ms: number): Promise<void>;
399
400 /**
401 * Returns a function that will wrap the given `nodeFunction`. 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. The node function should conform to node.js convention of accepting a callback as last argument and calling that callback with error as the first argument and success value on the second argument.
402 *
403 * If the `nodeFunction` calls its callback with multiple success values, the fulfillment value will be an array of them.
404 *
405 * If you pass a `receiver`, the `nodeFunction` will be called as a method on the `receiver`.
406 */
407 // TODO how to model promisify?
408 static promisify(nodeFunction: Function, receiver?: any): Function;
409
410 /**
411 * 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. The promisified method name will be the original method name postfixed with `Async`. Returns the input object.
412 *
413 * Note that the original methods on the object are not overwritten but new methods are created with the `Async`-postfix. For example, if you `promisifyAll()` the node.js `fs` object use `fs.statAsync()` to call the promisified `stat` method.
414 */
415 // TODO how to model promisifyAll?
416 static promisifyAll(target: Object): Object;
417
418 /**
419 * Returns a function that can use `yield` to run asynchronous code synchronously. This feature requires the support of generators which are drafted in the next version of the language. Node version greater than `0.11.2` is required and needs to be executed with the `--harmony-generators` (or `--harmony`) command-line switch.
420 */
421 // TODO fix coroutine GeneratorFunction
422 static coroutine<R>(generatorFunction: Function): Function;
423
424 /**
425 * Spawn a coroutine which may yield promises to run asynchronous code synchronously. This feature requires the support of generators which are drafted in the next version of the language. Node version greater than `0.11.2` is required and needs to be executed with the `--harmony-generators` (or `--harmony`) command-line switch.
426 */
427 // TODO fix spawn GeneratorFunction
428 static spawn<R>(generatorFunction: Function): Promise<R>;
429
430 /**
431 * This is relevant to browser environments with no module loader.
432 *
433 * Release control of the `Promise` namespace to whatever it was before this library was loaded. Returns a reference to the library namespace so you can attach it to something else.
434 */
435 static noConflict(): typeof Promise;
436
437 /**
438 * 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.
439 *
440 * Passing no value or a non-function will have the effect of removing any kind of handling for possibly unhandled rejections.
441 */
442 static onPossiblyUnhandledRejection(handler: (reason: any) => any): void;
443
444 /**
445 * 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. The promise's fulfillment value is an array with fulfillment values at respective positions to the original array. If any promise in the array rejects, the returned promise is rejected with the rejection reason.
446 */
447 // TODO enable more overloads
448 // promise of array with promises of value
449 static all<R>(values: Promise.Thenable<Promise.Thenable<R>[]>): Promise<R[]>;
450 // promise of array with values
451 static all<R>(values: Promise.Thenable<R[]>): Promise<R[]>;
452 // array with promises of value
453 static all<R>(values: Promise.Thenable<R>[]): Promise<R[]>;
454 // array with values
455 static all<R>(values: R[]): Promise<R[]>;
456
457 /**
458 * 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. The promise's fulfillment value is an object with fulfillment values at respective keys to the original object. If any promise in the object rejects, the returned promise is rejected with the rejection reason.
459 *
460 * If `object` is a trusted `Promise`, then it will be treated as a promise for object rather than for its properties. All other objects are treated for their properties as is returned by `Object.keys` - the object's own enumerable properties.
461 *
462 * *The original object is not modified.*
463 */
464 // TODO verify this is correct
465 // trusted promise for object
466 static props(object: Promise<Object>): Promise<Object>;
467 // object
468 static props(object: Object): Promise<Object>;
469
470 /**
471 * 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 either fulfilled or rejected. The fulfillment value is an array of ``PromiseInspection`` instances at respective positions in relation to the input array.
472 *
473 * *original: The array is not modified. The input array sparsity is retained in the resulting array.*
474 */
475 // promise of array with promises of value
476 static settle<R>(values: Promise.Thenable<Promise.Thenable<R>[]>): Promise<Promise.Inspection<R>[]>;
477 // promise of array with values
478 static settle<R>(values: Promise.Thenable<R[]>): Promise<Promise.Inspection<R>[]>;
479 // array with promises of value
480 static settle<R>(values: Promise.Thenable<R>[]): Promise<Promise.Inspection<R>[]>;
481 // array with values
482 static settle<R>(values: R[]): Promise<Promise.Inspection<R>[]>;
483
484 /**
485 * 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.
486 */
487 // promise of array with promises of value
488 static any<R>(values: Promise.Thenable<Promise.Thenable<R>[]>): Promise<R>;
489 // promise of array with values
490 static any<R>(values: Promise.Thenable<R[]>): Promise<R>;
491 // array with promises of value
492 static any<R>(values: Promise.Thenable<R>[]): Promise<R>;
493 // array with values
494 static any<R>(values: R[]): Promise<R>;
495
496 /**
497 * 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 or rejected as soon as a promise in the array is fulfilled or rejected with the respective rejection reason or fulfillment value.
498 *
499 * **Note** If you pass empty array or a sparse array with no values, or a promise/thenable for such, it will be forever pending.
500 */
501 // promise of array with promises of value
502 static race<R>(values: Promise.Thenable<Promise.Thenable<R>[]>): Promise<R>;
503 // promise of array with values
504 static race<R>(values: Promise.Thenable<R[]>): Promise<R>;
505 // array with promises of value
506 static race<R>(values: Promise.Thenable<R>[]): Promise<R>;
507 // array with values
508 static race<R>(values: R[]): Promise<R>;
509
510 /**
511 * Initiate a competetive race between multiple promises or values (values will become immediately fulfilled promises). When `count` amount of promises have been fulfilled, the returned promise is fulfilled with an array that contains the fulfillment values of the winners in order of resolution.
512 *
513 * If too many promises are rejected so that the promise can never become fulfilled, it will be immediately rejected with an array of rejection reasons in the order they were thrown in.
514 *
515 * *The original array is not modified.*
516 */
517 // promise of array with promises of value
518 static some<R>(values: Promise.Thenable<Promise.Thenable<R>[]>, count: number): Promise<R[]>;
519 // promise of array with values
520 static some<R>(values: Promise.Thenable<R[]>, count: number): Promise<R[]>;
521 // array with promises of value
522 static some<R>(values: Promise.Thenable<R>[], count: number): Promise<R[]>;
523 // array with values
524 static some<R>(values: R[], count: number): Promise<R[]>;
525
526 /**
527 * Like `Promise.all()` but instead of having to pass an array, the array is generated from the passed variadic arguments.
528 */
529 // variadic array with promises of value
530 static join<R>(...values: Promise.Thenable<R>[]): Promise<R[]>;
531 // variadic array with values
532 static join<R>(...values: R[]): Promise<R[]>;
533
534 /**
535 * Map an array, or a promise of an array, which contains a promises (or a mix of promises and values) with the given `mapper` function with the signature `(item, index, arrayLength)` where `item` is the resolved value of a respective promise in the input array. If any promise in the input array is rejected the returned promise is rejected as well.
536 *
537 * If the `mapper` function returns promises or thenables, the returned promise will wait for all the mapped results to be resolved as well.
538 *
539 * *The original array is not modified.*
540 */
541 // promise of array with promises of value
542 static map<R, U>(values: Promise.Thenable<Promise.Thenable<R>[]>, mapper: (item: R, index: number, arrayLength: number) => Promise.Thenable<U>): Promise<U[]>;
543 static map<R, U>(values: Promise.Thenable<Promise.Thenable<R>[]>, mapper: (item: R, index: number, arrayLength: number) => U): Promise<U[]>;
544
545 // promise of array with values
546 static map<R, U>(values: Promise.Thenable<R[]>, mapper: (item: R, index: number, arrayLength: number) => Promise.Thenable<U>): Promise<U[]>;
547 static map<R, U>(values: Promise.Thenable<R[]>, mapper: (item: R, index: number, arrayLength: number) => U): Promise<U[]>;
548
549 // array with promises of value
550 static map<R, U>(values: Promise.Thenable<R>[], mapper: (item: R, index: number, arrayLength: number) => Promise.Thenable<U>): Promise<U[]>;
551 static map<R, U>(values: Promise.Thenable<R>[], mapper: (item: R, index: number, arrayLength: number) => U): Promise<U[]>;
552
553 // array with values
554 static map<R, U>(values: R[], mapper: (item: R, index: number, arrayLength: number) => Promise.Thenable<U>): Promise<U[]>;
555 static map<R, U>(values: R[], mapper: (item: R, index: number, arrayLength: number) => U): Promise<U[]>;
556
557 /**
558 * Reduce an array, or a promise of an array, which contains a promises (or a mix of promises and values) with the given `reducer` function with the signature `(total, current, index, arrayLength)` where `item` is the resolved value of a respective promise in the input array. If any promise in the input array is rejected the returned promise is rejected as well.
559 *
560 * If the reducer function returns a promise or a thenable, the result for the promise is awaited for before continuing with next iteration.
561 *
562 * *The original array is not modified. If no `intialValue` is given and the array doesn't contain at least 2 items, the callback will not be called and `undefined` is returned. If `initialValue` is given and the array doesn't have at least 1 item, `initialValue` is returned.*
563 */
564 // promise of array with promises of value
565 static reduce<R, U>(values: Promise.Thenable<Promise.Thenable<R>[]>, reducer: (total: U, current: R, index: number, arrayLength: number) => Promise.Thenable<U>, initialValue?: U): Promise<U>;
566 static reduce<R, U>(values: Promise.Thenable<Promise.Thenable<R>[]>, reducer: (total: U, current: R, index: number, arrayLength: number) => U, initialValue?: U): Promise<U>;
567
568 // promise of array with values
569 static reduce<R, U>(values: Promise.Thenable<R[]>, reducer: (total: U, current: R, index: number, arrayLength: number) => Promise.Thenable<U>, initialValue?: U): Promise<U>;
570 static reduce<R, U>(values: Promise.Thenable<R[]>, reducer: (total: U, current: R, index: number, arrayLength: number) => U, initialValue?: U): Promise<U>;
571
572 // array with promises of value
573 static reduce<R, U>(values: Promise.Thenable<R>[], reducer: (total: U, current: R, index: number, arrayLength: number) => Promise.Thenable<U>, initialValue?: U): Promise<U>;
574 static reduce<R, U>(values: Promise.Thenable<R>[], reducer: (total: U, current: R, index: number, arrayLength: number) => U, initialValue?: U): Promise<U>;
575
576 // array with values
577 static reduce<R, U>(values: R[], reducer: (total: U, current: R, index: number, arrayLength: number) => Promise.Thenable<U>, initialValue?: U): Promise<U>;
578 static reduce<R, U>(values: R[], reducer: (total: U, current: R, index: number, arrayLength: number) => U, initialValue?: U): Promise<U>;
579
580 /**
581 * Filter an array, or a promise of an array, which contains a promises (or a mix of promises and values) with the given `filterer` function with the signature `(item, index, arrayLength)` where `item` is the resolved value of a respective promise in the input array. If any promise in the input array is rejected the returned promise is rejected as well.
582 *
583 * 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.
584 *
585 * *The original array is not modified.
586 */
587 // promise of array with promises of value
588 static filter<R>(values: Promise.Thenable<Promise.Thenable<R>[]>, filterer: (item: R, index: number, arrayLength: number) => Promise.Thenable<boolean>): Promise<R[]>;
589 static filter<R>(values: Promise.Thenable<Promise.Thenable<R>[]>, filterer: (item: R, index: number, arrayLength: number) => boolean): Promise<R[]>;
590
591 // promise of array with values
592 static filter<R>(values: Promise.Thenable<R[]>, filterer: (item: R, index: number, arrayLength: number) => Promise.Thenable<boolean>): Promise<R[]>;
593 static filter<R>(values: Promise.Thenable<R[]>, filterer: (item: R, index: number, arrayLength: number) => boolean): Promise<R[]>;
594
595 // array with promises of value
596 static filter<R>(values: Promise.Thenable<R>[], filterer: (item: R, index: number, arrayLength: number) => Promise.Thenable<boolean>): Promise<R[]>;
597 static filter<R>(values: Promise.Thenable<R>[], filterer: (item: R, index: number, arrayLength: number) => boolean): Promise<R[]>;
598
599 // array with values
600 static filter<R>(values: R[], filterer: (item: R, index: number, arrayLength: number) => Promise.Thenable<boolean>): Promise<R[]>;
601 static filter<R>(values: R[], filterer: (item: R, index: number, arrayLength: number) => boolean): Promise<R[]>;
602}
603
604declare module Promise {
605 export interface RangeError extends Error {
606 }
607 export interface CancellationError extends Error {
608 }
609 export interface TimeoutError extends Error {
610 }
611 export interface TypeError extends Error {
612 }
613 export interface RejectionError extends Error {
614 }
615 export interface OperationalError extends Error {
616 }
617
618 // Ideally, we'd define e.g. "export class RangeError extends Error {}",
619 // but as Error is defined as an interface (not a class), TypeScript doesn't
620 // allow extending Error, only implementing it.
621 // However, if we want to catch() only a specific error type, we need to pass
622 // a constructor function to it. So, as a workaround, we define them here as such.
623 export function RangeError(): RangeError;
624 export function CancellationError(): CancellationError;
625 export function TimeoutError(): TimeoutError;
626 export function TypeError(): TypeError;
627 export function RejectionError(): RejectionError;
628 export function OperationalError(): OperationalError;
629
630 export interface Thenable<R> {
631 then<U>(onFulfilled: (value: R) => Thenable<U>, onRejected: (error: any) => Thenable<U>): Thenable<U>;
632 then<U>(onFulfilled: (value: R) => Thenable<U>, onRejected?: (error: any) => U): Thenable<U>;
633 then<U>(onFulfilled: (value: R) => U, onRejected: (error: any) => Thenable<U>): Thenable<U>;
634 then<U>(onFulfilled?: (value: R) => U, onRejected?: (error: any) => U): Thenable<U>;
635 }
636
637 export interface Resolver<R> {
638 /**
639 * Returns a reference to the controlled promise that can be passed to clients.
640 */
641 promise: Promise<R>;
642
643 /**
644 * 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.
645 */
646 resolve(value: R): void;
647 resolve(): void;
648
649 /**
650 * Reject the underlying promise with `reason` as the rejection reason.
651 */
652 reject(reason: any): void;
653
654 /**
655 * Progress the underlying promise with `value` as the progression value.
656 */
657 progress(value: any): void;
658
659 /**
660 * Gives you a callback representation of the `PromiseResolver`. Note that this is not a method but a property. The callback accepts error object in first argument and success values on the 2nd parameter and the rest, I.E. node js conventions.
661 *
662 * If the the callback is called with multiple success values, the resolver fullfills its promise with an array of the values.
663 */
664 // TODO specify resolver callback
665 callback: (err: any, value: R, ...values: R[]) => void;
666 }
667
668 export interface Inspection<R> {
669 /**
670 * See if the underlying promise was fulfilled at the creation time of this inspection object.
671 */
672 isFulfilled(): boolean;
673
674 /**
675 * See if the underlying promise was rejected at the creation time of this inspection object.
676 */
677 isRejected(): boolean;
678
679 /**
680 * See if the underlying promise was defer at the creation time of this inspection object.
681 */
682 isPending(): boolean;
683
684 /**
685 * Get the fulfillment value of the underlying promise. Throws if the promise wasn't fulfilled at the creation time of this inspection object.
686 *
687 * throws `TypeError`
688 */
689 value(): R;
690
691 /**
692 * Get the rejection reason for the underlying promise. Throws if the promise wasn't rejected at the creation time of this inspection object.
693 *
694 * throws `TypeError`
695 */
696 reason(): any;
697 }
698
699 /**
700 * Changes how bluebird schedules calls a-synchronously.
701 *
702 * @param scheduler Should be a function that asynchronously schedules
703 * the calling of the passed in function
704 */
705 export function setScheduler(scheduler: (callback: (...args: any[]) => void) => void): void;
706}
707
708declare module 'bluebird' {
709 export = Promise;
710}