Options
All
  • Public
  • Public/Protected
  • All
Menu

@jahed/promises - v1.0.1

Index

Functions

branch

  • branch<I, R1, R2>(onResolved: function, onRejected: function): (Anonymous function)
  • Calls a function based on whether a given promise resolves or rejects.

    This is a functional way of doing a promise.then, making it reusable.

    Type parameters

    • I

    • R1

    • R2

    Parameters

    • onResolved: function
        • (value: I): Promise<R1> | R1
        • Parameters

          • value: I

          Returns Promise<R1> | R1

    • onRejected: function
        • (error: Error): Promise<R2> | R2
        • Parameters

          • error: Error

          Returns Promise<R2> | R2

    Returns (Anonymous function)

coalesce

  • coalesce<V>(...suppliers: function[]): Promise<V>
  • Resolves to the first function that resolves, otherwise rejects.

    Type parameters

    • V

    Parameters

    • Rest ...suppliers: function[]

    Returns Promise<V>

compose

  • compose(...steps: function[]): (Anonymous function)
  • Calls the given functions in sequence, passing the result onto the next as a Promise rather than a value.

    Unlike waterfall which passes resolved values, you can use this to combine other Promise consuming functions like branch.

    Parameters

    • Rest ...steps: function[]

    Returns (Anonymous function)

doNothing

  • doNothing(): (Anonymous function)
  • A function that only resolves. Useful in branch and if you want to ignore errors.

    Returns (Anonymous function)

eventually

  • eventually<V>(fulfiller: function): Promise<V>
  • Returns a Promise which resolves or rejects based on a given function. This is a functional alternative to using new Promise().

    Type parameters

    • V

    Parameters

    • fulfiller: function
        • (resolve: function, reject: function): void
        • Parameters

          • resolve: function
              • (value: V): void
              • Parameters

                • value: V

                Returns void

          • reject: function
              • (error: Error): void
              • Parameters

                • error: Error

                Returns void

          Returns void

    Returns Promise<V>

every

  • every(promises: Promise<any>[] | object): Promise<object>
  • Takes an array or object map of promises and only resolves when all values resolve, rejecting otherwise.

    The resulting value is an array or object map of the resolved values from each Promise.

    If any of the promises reject, the entire function will reject with that reason.

    Passing an array is equivalent to using Promise.all().

    Parameters

    • promises: Promise<any>[] | object

    Returns Promise<object>

everyArray

  • everyArray(promises: Promise<any>[]): Promise<any[]>
  • Parameters

    • promises: Promise<any>[]

    Returns Promise<any[]>

everyObject

  • everyObject(promises: object): Promise<object>
  • Parameters

    • promises: object
      • [key: string]: Promise<any>

    Returns Promise<object>

not

  • not<I>(promise: Promise<I>): Promise<Error>
  • Reverses the result of a Promise. So a rejected Promise becomes resolved and a resolved Promise becomes rejected.

    When resolved, it provides the rejected error. When rejected, provides a generic error.

    Type parameters

    • I

    Parameters

    • promise: Promise<I>

    Returns Promise<Error>

reason

  • reason(message: string): Error
  • Creates an Error with the given message. This is a functional alternative to new Error().

    Parameters

    • message: string

    Returns Error

rejected

  • rejected(reason: Error): Promise<never>
  • Creates a rejected Promise with the given Error. Use reason() to neatly pass an error. This is an alternative to Promise.reject().

    Parameters

    • reason: Error

    Returns Promise<never>

relay

  • relay<I>(...steps: function[]): (Anonymous function)
  • Passes a given value to each function, finally resolving to the same value.

    If any of the functions reject, the entire function will reject with that reason.

    You can think of this like a Relay Race in sports. The baton is the passed to the next runner and doesn't change. If a runner drops the baton, the other runners won't receive the baton and the team fails.

    Type parameters

    • I

    Parameters

    • Rest ...steps: function[]

    Returns (Anonymous function)

resolveNullable

  • resolveNullable<V>(value?: V, error?: Error): Promise<V>
  • Resolves to a Promise which rejects if the given value is null or undefined, otherwise resolves with that value.

    Type parameters

    • V

    Parameters

    • Optional value: V
    • Default value error: Error = new Error(`value was ${value}`)

    Returns Promise<V>

resolved

  • resolved<V>(value: V): Promise<V>
  • Creates a resolved Promise with the given value. This is an alternative to Promise.resolve().

    Type parameters

    • V

    Parameters

    • value: V

    Returns Promise<V>

snowball

  • snowball(...steps: function[]): (Anonymous function)
  • Takes an object map and calls the given functions sequentially, assigning key-values from each step and passing the result onto the next.

    If any of the functions reject, the entire function will reject with that reason.

    This is useful if you're building up an object sequentially instead of at once like you might do with every.

    Parameters

    • Rest ...steps: function[]

    Returns (Anonymous function)

waterfall

  • waterfall(...steps: function[]): (Anonymous function)
  • Calls the given function sequentially as each Promise resolves, passing the value onto the next.

    If any of the functions reject, the entire function will reject with that reason.

    Parameters

    • Rest ...steps: function[]

    Returns (Anonymous function)

Generated using TypeDoc