/*! * The code following this comment originates from: * https://github.com/types/npm-bluebird * * Note for browser users: use bluebird-global typings instead of this one * if you want to use Bluebird via the global Promise symbol. * * Licensed under: * The MIT License (MIT) * * Copyright (c) 2016 unional * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ type Constructor = new (...args: any[]) => E; type CatchFilter = ((error: E) => boolean) | (object & E); type Resolvable = R | PromiseLike; type IterateFunction = (item: T, index: number, arrayLength: number) => Resolvable; declare class NativeBird extends Promise implements PromiseLike, NativeBird.Inspection { readonly [Symbol.toStringTag]: "Object"; /** * 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. * * If promise cancellation is enabled, passed in function will receive * one more function argument `onCancel` that allows to register an optional cancellation callback. */ constructor(callback: (resolve: (thenableOrResult?: Resolvable) => void, reject: (error?: any) => void, onCancel?: (callback: () => void) => void) => void); /** * Promises/A+ `.then()`. Returns a new promise chained from this promise. * * The new promise will be rejected or resolved depending on the passed `fulfilledHandler`, `rejectedHandler` and the state of this promise. */ // Based on PromiseLike.then, but returns a Bluebird instance. then(onFulfill?: (value: R) => Resolvable, onReject?: (error: any) => Resolvable): NativeBird; // For simpler signature help. then( onfulfilled?: ((value: R) => Resolvable) | null, onrejected?: ((reason: any) => Resolvable) | null ): NativeBird; /** * 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. * * Alias `.caught();` for compatibility with earlier ECMAScript version. */ catch(onReject: ((error: any) => Resolvable) | undefined | null): NativeBird; /** * 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. * * 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. * * Alias `.caught();` for compatibility with earlier ECMAScript version. */ catch( filter1: Constructor, filter2: Constructor, filter3: Constructor, filter4: Constructor, filter5: Constructor, onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable, ): NativeBird; catch( filter1: Constructor | CatchFilter, filter2: Constructor | CatchFilter, filter3: Constructor | CatchFilter, filter4: Constructor | CatchFilter, filter5: Constructor | CatchFilter, onReject: (error: E1 | E2 | E3 | E4 | E5) => Resolvable, ): NativeBird; catch( filter1: Constructor, filter2: Constructor, filter3: Constructor, filter4: Constructor, onReject: (error: E1 | E2 | E3 | E4) => Resolvable, ): NativeBird; catch( filter1: Constructor | CatchFilter, filter2: Constructor | CatchFilter, filter3: Constructor | CatchFilter, filter4: Constructor | CatchFilter, onReject: (error: E1 | E2 | E3 | E4) => Resolvable, ): NativeBird; catch( filter1: Constructor, filter2: Constructor, filter3: Constructor, onReject: (error: E1 | E2 | E3) => Resolvable, ): NativeBird; catch( filter1: Constructor | CatchFilter, filter2: Constructor | CatchFilter, filter3: Constructor | CatchFilter, onReject: (error: E1 | E2 | E3) => Resolvable, ): NativeBird; catch( filter1: Constructor, filter2: Constructor, onReject: (error: E1 | E2) => Resolvable, ): NativeBird; catch( filter1: Constructor | CatchFilter, filter2: Constructor | CatchFilter, onReject: (error: E1 | E2) => Resolvable, ): NativeBird; catch( filter1: Constructor, onReject: (error: E1) => Resolvable, ): NativeBird; catch( // tslint:disable-next-line:unified-signatures filter1: Constructor | CatchFilter, onReject: (error: E1) => Resolvable, ): NativeBird; /** * 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. */ error(onReject: (reason: any) => Resolvable): NativeBird; /** * 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. * * Alias `.lastly();` for compatibility with earlier ECMAScript version. */ finally(handler: () => Resolvable): NativeBird; /** * Like `.finally()`, but not called for rejections. */ tap(onFulFill: (value: R) => Resolvable): NativeBird; /** * Same as calling `Promise.delay(ms, this)`. */ delay(ms: number): NativeBird; /** * 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 TimeoutError or the error as the reason. * * You may specify a custom error message with the `message` parameter. */ timeout(ms: number, message?: string | Error): NativeBird; /** * 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. */ spread(this: NativeBird>, fulfilledHandler: (...values: Q[]) => Resolvable): NativeBird; /** * 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. */ all(this: NativeBird<[Resolvable, Resolvable, Resolvable, Resolvable, Resolvable]>): NativeBird<[T1, T2, T3, T4, T5]>; all(this: NativeBird<[Resolvable, Resolvable, Resolvable, Resolvable]>): NativeBird<[T1, T2, T3, T4]>; all(this: NativeBird<[Resolvable, Resolvable, Resolvable]>): NativeBird<[T1, T2, T3]>; all(this: NativeBird<[Resolvable, Resolvable]>): NativeBird<[T1, T2]>; all(this: NativeBird<[Resolvable]>): NativeBird<[T1]>; all(this: NativeBird>>): NativeBird; /** * 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. */ all(): NativeBird; /** * 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. */ any(this: NativeBird>): NativeBird; /** * 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. */ any(): NativeBird; /** * 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. */ race(this: NativeBird>): NativeBird; /** * 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. */ race(): NativeBird; /** * 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. */ map(this: NativeBird>, mapper: IterateFunction, options?: NativeBird.ConcurrencyOption): NativeBird; /** * Same as calling `Bluebird.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. */ reduce(this: NativeBird>, reducer: (memo: U, item: Q, index: number, arrayLength: number) => Resolvable, initialValue?: U): NativeBird; /** * 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. */ each(this: NativeBird>, iterator: IterateFunction): NativeBird; /** * 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. */ mapSeries(this: NativeBird>, iterator: IterateFunction): NativeBird; /** * Register a node-style callback on this promise. * @param fn */ asCallback(fn: (err: unknown, result?: unknown) => void): NativeBird /** * Start the chain of promises with `Promise.try`. Any synchronous exceptions will be turned into rejections on the returned promise. * * 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. * * Alias for `attempt();` for compatibility with earlier ECMAScript version. */ static try(fn: () => Resolvable): NativeBird; /** * Create a promise that is resolved with the given `value`. If `value` is a thenable or promise, the returned promise will assume its state. */ static resolve(): NativeBird; static resolve(value: Resolvable): NativeBird; /** * Create a promise that is rejected with the given `reason`. */ static reject(reason: any): NativeBird; /** * @deprecated * Create a promise with undecided fate and return a `PromiseResolver` to control it. See resolution?: Promise(#promise-resolution). * @see http://bluebirdjs.com/docs/deprecated-apis.html#promise-resolution */ static defer(): NativeBird.Resolver; /** * Returns a promise that will be resolved 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. */ static delay(ms: number, value: Resolvable): NativeBird; static delay(ms: number): NativeBird; /** * 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. */ // TODO enable more overloads // array with promises of different types static all(values: [Resolvable, Resolvable, Resolvable, Resolvable, Resolvable]): NativeBird<[T1, T2, T3, T4, T5]>; static all(values: [Resolvable, Resolvable, Resolvable, Resolvable]): NativeBird<[T1, T2, T3, T4]>; static all(values: [Resolvable, Resolvable, Resolvable]): NativeBird<[T1, T2, T3]>; static all(values: [Resolvable, Resolvable]): NativeBird<[T1, T2]>; static all(values: [Resolvable]): NativeBird<[T1]>; // array with values static all(values: Resolvable>>): NativeBird; /** * 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. */ static any(values: Resolvable>>): NativeBird; /** * 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. * * **Note** If you pass empty array or a sparse array with no values, or a promise/thenable for such, it will be forever pending. */ static race(values: Resolvable>>): NativeBird; /** * 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. * * If the `mapper` function returns promises or thenables, the returned promise will wait for all the mapped results to be resolved as well. * * *The original array is not modified.* */ static map( values: Resolvable>>, mapper: IterateFunction, options?: NativeBird.ConcurrencyOption ): NativeBird; /** * 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. * * If the reducer function returns a promise or a thenable, the result for the promise is awaited for before continuing with next iteration. * * *The original array is not modified. If no `initialValue` 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.* */ static reduce( values: Resolvable>>, reducer: (total: U, current: R, index: number, arrayLength: number) => Resolvable, initialValue?: U ): NativeBird; /** * Iterate over an array, or a promise of an array, * which contains promises (or a mix of promises and values) with the given iterator function with the signature `(item, index, value)` * where item is the resolved value of a respective promise in the input array. * Iteration happens serially. If any promise in the input array is rejected the returned promise is rejected as well. * * Resolves to the original array unmodified, this method is meant to be used for side effects. * If the iterator function returns a promise or a thenable, the result for the promise is awaited for before continuing with next iteration. */ static each( values: Resolvable>>, iterator: IterateFunction ): NativeBird; /** * Given an Iterable(arrays are Iterable), or a promise of an Iterable, which produces promises (or a mix of promises and values), * iterate over all the values in the Iterable into an array and iterate over the array serially, in-order. * * Returns a promise for an array that contains the values returned by the iterator function in their respective positions. * 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. * This results in a mapSeries kind of utility but it can also be used simply as a side effect iterator similar to Array#forEach. * * 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. */ static mapSeries( values: Resolvable>>, iterator: IterateFunction ): NativeBird; /** * 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. * If promise cancellation is enabled, passed in function will receive one more function argument `onCancel` that allows to register an optional cancellation callback. */ static Promise: typeof NativeBird; } declare namespace NativeBird { interface ConcurrencyOption { concurrency: number; } interface SpreadOption { spread: boolean; } interface FromNodeOptions { multiArgs?: boolean; } interface PromisifyOptions { context?: any; multiArgs?: boolean; } interface PromisifyAllOptions extends PromisifyOptions { suffix?: string; filter?(name: string, func: (...args: any[]) => any, target?: any, passesDefaultFilter?: boolean): boolean; // The promisifier gets a reference to the original method and should return a function which returns a promise promisifier?(this: T, originalMethod: (...args: any[]) => any, defaultPromisifer: (...args: any[]) => (...args: any[]) => NativeBird): () => PromiseLike; } interface CoroutineOptions { yieldHandler(value: any): any; } type ResolvableProps = object & {[K in keyof T]: Resolvable}; interface Resolver { /** * Returns a reference to the controlled promise that can be passed to clients. */ promise: NativeBird; /** * 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. */ resolve(value: R): void; resolve(): void; /** * Reject the underlying promise with `reason` as the rejection reason. */ reject(reason: any): void; /** * 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. * * If the the callback is called with multiple success values, the resolver fulfills its promise with an array of the values. */ // TODO specify resolver callback callback(err: any, value: R, ...values: R[]): void; } interface Inspection {} } export = NativeBird;