Options
All
  • Public
  • Public/Protected
  • All
Menu

Class FunPromise<T>

The class that you should use instead of Promise. It implements the Promise API, so it should be a drop-in replacement.

Type parameters

  • T

Hierarchy

  • FunPromise

Implements

  • Promise<T>

Index

Constructors

constructor

Properties

Protected Readonly wrapped

wrapped: Promise<T>

The promise that was wrapped after attaching our custom logic.

Accessors

[Symbol.toStringTag]

  • get [Symbol.toStringTag](): string
  • 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.

    Returns string

Methods

all

arrayify

  • arrayify(resolveValues?: boolean, sequentialResolution?: boolean): FunPromise<Item<T>[]>
  • Coerces the resolve value (which must be an Iterable) into an array. The Iterable requirement comes from the Item<T> return value: Item<T> 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.

    Parameters

    • Default value resolveValues: boolean = false
    • Default value sequentialResolution: boolean = false

    Returns FunPromise<Item<T>[]>

cancel

  • cancel(): this
  • Cancel the FunPromise. A cancelled FunPromise will silently disregard any resolution or rejection which occurs after the cancellation.

    Returns this

catch

  • 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.

    Type parameters

    • TResult = never

    Parameters

    • Default value onrejected: (reason: unknown) => Promisable<TResult> = _identity

      The callback to execute when the Promise is rejected.

    Returns FunPromise<T | TResult>

    A Promise for the completion of the callback.

delay

  • 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.

    Type parameters

    • T = void

    Parameters

    • waitTimeMs: number
    • Optional returnValue: Promisable<T>

    Returns FunPromise<T>

filter

  • 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].

    Parameters

    Returns FunPromise<Item<T>[]>

finally

flatFold

  • 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.

    Type parameters

    • T2 = Item<T>

    Parameters

    Returns FunPromise<T2[]>

flatMap

  • 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.

    Type parameters

    • T2 = Item<T>

    Parameters

    Returns FunPromise<T2[]>

fold

  • 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.

    Type parameters

    • T2 = Item<T>

    Parameters

    Returns FunPromise<T2>

isCancelled

  • isCancelled(): boolean
  • Returns whether or not the promise has been cancelled. See cancel() for more details.

    Returns boolean

map

  • 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.

    Type parameters

    • T2 = Item<T>

    Parameters

    Returns FunPromise<T2[]>

reject

  • 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.

    Parameters

    • Optional value: unknown

    Returns FunPromise<never>

resolve

  • Takes a value (or a promise of a value) and resolves to the new value, disregarding any previous resolution value.

    Type parameters

    • T2 = void

    Parameters

    Returns FunPromise<T2>

return

settle

settleAll

simplify

  • 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<Unpromise<T>> instead of just Promise<T>, 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.

    Returns FunPromise<Unpromise<T>>

simplifyAll

tap

  • Access the resolved value without changing it. Note that if the callback rejects (ie: throws), then the resulting promise will be rejected.

    Parameters

    Returns FunPromise<T>

tapCatch

  • 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]].

    Parameters

    Returns FunPromise<T>

tapEach

then

  • Attaches callbacks for the resolution and/or rejection of the Promise.

    Type parameters

    • TResult1 = T

    Parameters

    • onfulfilled: (value: T) => Promisable<TResult1>

      The optional callback to execute when the Promise is resolved.

    Returns FunPromise<TResult1>

    A Promise for the completion of which ever callback is executed.

  • Type parameters

    • TResult1 = T

    • TResult2 = TResult1

    Parameters

    Returns FunPromise<TResult1 | TResult2>

wrapError

  • Handles rejections like 'catch', but wraps them in a [[NestedError]] with the given message.

    Parameters

    • msg: string

    Returns FunPromise<T>

wrapErrors

  • 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]].

    Parameters

    • msg: string

    Returns FunPromise<Item<T>[]>

Static all

Static coalesce

  • Given a PromisableIterable whose values are nullary functions returning Promisable<T>, 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.

    Type parameters

    • T

    Parameters

    Returns FunPromise<T>

Static delay

  • 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.

    Parameters

    • waitTimeMs: number

    Returns FunPromise<void>

  • Type parameters

    • T

    Parameters

    Returns FunPromise<T>

Static filter

Static flatFold

Static flatMap

Static fold

Static map

Static reject

  • 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.

    Parameters

    • Optional value: unknown

    Returns FunPromise<never>

Static resolve

Static return

  • An alias for resolve provided for consistency with the instance return method.

    deprecated

    Use resolve instead.

    Type parameters

    • T = void

    Parameters

    Returns FunPromise<T>

Static settleAll

Static try

Generated using TypeDoc