/**
* @format
*/
///
///
///
///
///
///
///
///
///
import { Promisable, PromisableIterable, Unpromise, Item, Settlement } from "./types";
/**
* The class that you should use instead of `Promise`. It implements the `Promise` API, so it should be a drop-in replacement.
*/
export default class FunPromise implements Promise {
/**
* Whether or not this FunPromise has been cancelled.
*/
private _isCancelled;
/**
* The promise that was wrapped after attaching our custom logic.
*/
protected readonly wrapped: Promise;
/**
* Constructor, which takes the promise to wrap.
*/
constructor(wrapped: Promisable);
/**
* Takes a value (or a promise of a value) and returns a promise wrapping
* it.
*/
static resolve(value?: Promisable): FunPromise;
/**
* Takes a value (or a promise of a value) and resolves to the new value,
* disregarding any previous resolution value.
*/
resolve(value?: Promisable): FunPromise;
/**
* An alias for `resolve` provided for consistency with the instance `return` method.
*
* @deprecated Use `resolve` instead.
*/
static return(value?: Promisable): FunPromise;
/**
* An alias for `resolve` provided for compatibility with [the Bluebird API|http://bluebirdjs.com/docs/api/return.html].
*
* @deprecated Use `resolve` instead.
*/
return(value?: Promisable): FunPromise;
/**
* Takes a value (or a promise of a value) and returns a promise rejecting
* with that value, after unwrapping as many layers of `PromiseLike`
* wrappers as necessary.
*/
static reject(value?: unknown): FunPromise;
/**
* Takes a value (or a promise of a value) and returns a promise rejecting
* with that value, after unwrapping as many layers of `PromiseLike`
* wrappers as necessary. This disregards any existing status.
*/
reject(value?: unknown): FunPromise;
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The optional callback to execute when the Promise is resolved.
* @param onrejected The optional callback to execute when the Promise is rejected. If provided, the result of this callback is the new resolution value, and this promise is resolved, not rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then(onfulfilled: (value: T) => Promisable): FunPromise;
then(onfulfilled: (value: T) => Promisable, onrejected: (reason: unknown) => Promisable): FunPromise;
/**
* Attaches a callback for only the rejection of the Promise. If the callback throws, then throws a [[`NestedError`]] with
* both the original rejection reason and the new thrown value.
*
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of the callback.
*/
catch(onrejected?: (reason: unknown) => Promisable): FunPromise;
/**
* Creates a Promise that is resolved with an array of results when all of the
* provided Promises resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
static all(values: PromisableIterable): FunPromise;
static all(values: [
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable
]): FunPromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;
static all(values: [
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable
]): FunPromise<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;
static all(values: [
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable
]): FunPromise<[T1, T2, T3, T4, T5, T6, T7, T8]>;
static all(values: [
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable
]): FunPromise<[T1, T2, T3, T4, T5, T6, T7]>;
static all(values: [
Promisable,
Promisable,
Promisable,
Promisable,
Promisable,
Promisable
]): FunPromise<[T1, T2, T3, T4, T5, T6]>;
static all(values: [
Promisable,
Promisable,
Promisable,
Promisable,
Promisable
]): FunPromise<[T1, T2, T3, T4, T5]>;
static all(values: [Promisable, Promisable, Promisable, Promisable]): FunPromise<[T1, T2, T3, T4]>;
static all(values: [Promisable, Promisable, Promisable]): FunPromise<[T1, T2, T3]>;
static all(values: [Promisable, Promisable]): FunPromise<[T1, T2]>;
static all(values: [Promisable]): FunPromise<[T1]>;
all(): FunPromise- []>;
/**
* Given a function (or a promise of a function) that returns a value (or a promise of a value),
* create a promise that executes the function and returns the value. If executing the function
* throws an exception, then that exception becomes the rejection of the promise.
*
* Any arguments after the first will be passed into the function when it is invoked. If they are
* a `PromiseLike`, then they will be resolved and the resolution value will be passed into the
* function instead.
*
* This function is really useful in the following cases:
* 1. to [avoid releasing Zalgo](https://blog.izs.me/2013/08/designing-apis-for-asynchrony)
* 2. when you want to create a `FunPromise` based on an `async` function
* 3. when you want to create a `FunPromise` based on a normal function
*/
static try(source: Promisable<(arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6, arg7: Arg7, arg8: Arg8) => Promisable>, arg1: Promisable, arg2: Promisable, arg3: Promisable, arg4: Promisable, arg5: Promisable, arg6: Promisable, arg7: Promisable, arg8: Promisable): FunPromise;
static try(source: Promisable<(arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6, arg7: Arg7) => Promisable>, arg1: Promisable, arg2: Promisable, arg3: Promisable, arg4: Promisable, arg5: Promisable, arg6: Promisable, arg7: Promisable): FunPromise;
static try(source: Promisable<(arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5, arg6: Arg6) => Promisable>, arg1: Promisable, arg2: Promisable, arg3: Promisable, arg4: Promisable, arg5: Promisable, arg6: Promisable): FunPromise;
static try(source: Promisable<(arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4, arg5: Arg5) => Promisable>, arg1: Promisable, arg2: Promisable, arg3: Promisable, arg4: Promisable, arg5: Promisable): FunPromise;
static try(source: Promisable<(arg1: Arg1, arg2: Arg2, arg3: Arg3, arg4: Arg4) => Promisable>, arg1: Promisable, arg2: Promisable, arg3: Promisable, arg4: Promisable): FunPromise;
static try(source: Promisable<(arg1: Arg1, arg2: Arg2, arg3: Arg3) => Promisable>, arg1: Promisable, arg2: Promisable, arg3: Promisable): FunPromise;
static try(source: Promisable<(arg1: Arg1, arg2: Arg2) => Promisable>, arg1: Promisable, arg2: Promisable): FunPromise;
static try(source: Promisable<(arg1: Arg1) => Promisable>, arg1: Promisable): FunPromise;
static try(source: Promisable<() => Promisable>): FunPromise;
/**
* Unwraps layers of `PromiseLike` wrappers as necessary.
*
* This behavior is actually part of the Promise/A+ spec, but the type system struggles with that fact,
* so this method is a workaround.
*
* In a future version of this library, `FunPromise` will extend `Promise>` instead of
* just `Promise`, which will render this method irrelevant. Unfortunately, Typescript's type system
* doesn't seem capable of handling that reality quite yet, so this is what we get.
*/
simplify(): FunPromise>;
/**
* For each element of the resolved iterable, unwraps layers of `PromiseLike` wrappers as necessary.
*/
simplifyAll(): FunPromise>[]>;
/**
* Coerces the resolve value (which must be an `Iterable`) into an array. The `Iterable` requirement
* comes from the `Item` return value: `Item` is equivalent to `never` if `T` is not an `Iterable`.
*
* Note that this function does *NOT* resolve the items within the array unless you pass the first argument
* as `true`. The items are not resolved sequentially unless you also pass a second argument as `true`.
*/
arrayify(resolveValues?: boolean, sequentialResolution?: boolean): FunPromise
- []>;
/**
* Given a mapping function, apply the mapping function to each element of the promise's resolved value,
* and return an array with the results of the mapping. If any of the mapping results are rejected,
* the entire operation will be rejected.
*
* The order of the elements in the result correspond to the order of the elements in the promise's
* resolved value. However, the resolution order is not guaranteed. For example, although the
* output at index 0 will hold the mapping of the input element at index 0, it is not guaranteed that
* the mapping of index 0 will be awaited before the mapping of index 1.
*/
map>(mapper: (it: Item) => Promisable): FunPromise;
/**
* Equivalent to `FunPromise.resolve(values).map(mapper)`.
*/
static map(values: PromisableIterable, mapper: (it: T) => Promisable): FunPromise;
/**
* Required to implement `Promise`, but you almost certainly don't care about it.
*
* All the same, it returns the string tag of the underlying promise.
*/
get [Symbol.toStringTag](): string;
/**
* Executes the provided code whether the promise rejects or resolves.
*/
finally(): FunPromise;
finally(onfinally: () => void): FunPromise;
/**
* Given a [[`PromisableIterable`]] whose values are nullary functions returning [[Promisable|`Promisable`]],
* this executes all the functions simultaneously and returns the first whose return value passes the provided test.
* The default test returns true if the value is not `null` or `undefined`.
*
* If no function resolves successfully, the last seen rejection is thrown. If some functions resolve but some reject,
* and none of the resolved values pass the test, then the last seen rejection is thrown.
*
* If all the functions resolve but to a value but no value passes the test, then this rejects with an error saying as much.
*/
static coalesce(fns: PromisableIterable<() => Promisable>, test?: (item: T) => Promisable): FunPromise;
/**
* Waits for `waitTimeMs` milliseconds before resolving. If `returnValue` is provided, resolves with the provided value.
*
* If `waitTimeMs` is less than or equal to zero, then it simply defers until the call stack is clear.
*/
static delay(waitTimeMs: number): FunPromise;
static delay(waitTimeMs: number, returnValue: Promisable): FunPromise;
/**
* Waits for `waitTimeMs` milliseconds before resolving. If `returnValue` is provided, resolves with the provided value.
*
* If `waitTimeMs` is less than or equal to zero, then it simply defers until the call stack is clear.
*/
delay(waitTimeMs: number, returnValue?: Promisable): FunPromise;
/**
* Given a filtering function, apply the filtering function to each element of the promise's resolved value,
* and return an array with the values for which the filtering function returns `true`. If any of the filtering
* results are rejected, the entire operation will be rejected.
*
* The order of the elements in the result are stable with regard to the order of the elements in the promise's
* resolved value. That is, if `X < Y` and `input[X]` and `input[Y]` are both in the input, and `input[X]` and
* `input[Y]` both pass the filtering function, then the output index of `input[X]` will be less than the output
* index of `input[Y]`. However, the resolution order is not guaranteed: that is, `input[Y]` may be resolved and
* tested before `input[X]` even though `input[X]` has a lower output index than `input[Y]`.
*/
filter(test: (it: Item) => Promisable): FunPromise
- []>;
/**
* Equivalent to `FunPromise.resolve(items).filter(test)`.
*/
static filter(items: PromisableIterable, test: (it: Item>) => Promisable): FunPromise
- >[]>;
/**
* Given a mapping function, apply the mapping function to each element of the promise's resolved value,
* and return an array with the concatenated results of the mapping. If any of the mapping results are
* rejected, the entire operation will be rejected.
*
* The order of the elements in the result correspond to the order of the elements in the promise's
* resolved value. However, the resolution order is not guaranteed.
*/
flatMap>(mapper: (it: Item) => Promisable): FunPromise;
/**
* Equivalent to `FunPromise.resolve(values).flatMap(mapper)`.
*/
static flatMap(values: PromisableIterable, mapper: (it: T) => Promisable): FunPromise;
/**
* Access the resolved value without changing it. Note that if the callback rejects (ie: throws),
* then the resulting promise will be rejected.
*/
tap(callback: (val: T) => Promisable): FunPromise;
/**
* Access the rejection reason without changing it. Note that if the callback itself rejects (ie: throws),
* both rejection reasons will be capture in a single [[`NestedError`]].
*/
tapCatch(callback: (reason: unknown) => Promisable): FunPromise;
/**
* Access each of the resolved values of a resolved iterable without changing it. Note that if the callback rejects (ie: throws),
* then the resulting promise will be rejected.
*/
tapEach(callback: (val: Item) => Promisable): FunPromise
- []>;
/**
* Given an initial value and an accumulator function, apply the accumlator function to each element of the promise's resolved value,
* passing in the current value and the result. Returns an array with the result of the accumulation. If any of the promise's values are
* rejected, the entire operation will be rejected.
*
* The resolution order is not guaranteed. The accumulator function will be passed values as those values resolve.
*/
fold>(initialValue: T2, accumulator: (memo: T2, it: Item) => Promisable): FunPromise;
/**
* Equivalent to `FunPromise.resolve(values).fold(initialValue, accumulator)`.
*/
static fold(values: PromisableIterable, initialValue: T2, accumulator: (memo: T2, it: T) => Promisable): FunPromise;
/**
* Given an initial array of values and an accumulator function, apply the accumlator function to each element of the promise's resolved value,
* passing in the current array of values and the resolved item. Returns an array with the concatenated results of the accumulation.
* If any of the promise's values are rejected, the entire operation will be rejected.
*
* The resolution order is not guaranteed. The accumulator function will be passed values as those values resolve.
*/
flatFold>(initialValue: PromisableIterable, accumulator: (memo: T2[], it: Item) => PromisableIterable): FunPromise;
/**
* Equivalent to `FunPromise.resolve(values).flatFold(initialValue, accumulator)`.
*/
static flatFold(values: PromisableIterable, initialValue: PromisableIterable, accumulator: (memo: T2[], it: T) => PromisableIterable): FunPromise;
/**
* Handles rejections like 'catch', but wraps them in a [[`NestedError`]] with the given message.
*/
wrapError(msg: string): FunPromise;
/**
* Resolves all the elements of the resolved value, which is assumed to be an `Iterable`. If any
* of the values reject, all the reasons are collected and wrapped in a [[`NestedError`]].
*/
wrapErrors(msg: string): FunPromise
- []>;
/**
* Cancel the FunPromise. A cancelled FunPromise will silently disregard any resolution or rejection which occurs after the cancellation.
*/
cancel(): this;
/**
* Returns whether or not the promise has been cancelled. See `cancel()` for more details.
*/
isCancelled(): boolean;
/**
* Captures either fulfillment or rejection and resolves an object that describes the result.
*/
settle(): FunPromise>;
/**
* Assuming that the resolved value is an iterable, then for each element of the
* array, captures either the fulfillment or rejection of that element.
*/
settleAll(): FunPromise>[]>;
/**
* Equivalent to `FunPromise.resolve(iterable).settleAll()`.
*/
static settleAll(iterable: PromisableIterable): FunPromise[]>;
}
//# sourceMappingURL=fun-promise.d.ts.map