Options
All
  • Public
  • Public/Protected
  • All
Menu

xaa

Index

References

Renames and re-exports makeDefer
Renames and re-exports tryCatch

Type aliases

MapContext<T>: { array: readonly T[]; failed?: boolean; assertNoFailure: any }

context from xaa.map to the mapper callback

Type parameters

  • T

Type declaration

  • array: readonly T[]

    the original array that's passed to xaa.map

  • Optional failed?: boolean

    indicate if another map operation during concurrent map has failed Mapping should observe this flag whenever possible and avoid continuing if it makes sense.

  • assertNoFailure:function
    • assertNoFailure(): void
    • During concurrent map, if any mapper failed, this allow other inflight map functions to assert that no failure has occurred, else stop.

      Returns void

MapFunction<T, O>: (value: Awaited<T>, index: number, context: MapContext<T>) => O | Promise<O>

Type parameters

  • T

  • O

Type declaration

    • (value: Awaited<T>, index: number, context: MapContext<T>): O | Promise<O>
    • callback function for xaa.map to map the value.

      Parameters

      • value: Awaited<T>

        value to map

      • index: number

        index of the value in the array

      • context: MapContext<T>

        MapContext

      Returns O | Promise<O>

      any or a promise

MapOptions: { concurrency: number; thisArg?: any }

options for xaa.map

Type declaration

  • concurrency: number

    number of items to map concurrently

  • Optional thisArg?: any

    the this passed to the map callback

Functions

  • delay(delayMs: number): Promise<void>
  • delay<T>(delayMs: number, valOrFunc: T): Promise<T>
  • delay some milliseconds and then return valOrFunc

    Sample:

    await xaa.delay(500);
    await xaa.delay(500, "Result");
    await xaa.delay(500, () => "Result");

    Parameters

    • delayMs: number

      number milliseconds to delay

    Returns Promise<void>

    valOrFunc or its returned value if it's a function.

  • Type parameters

    • T = void

    Parameters

    • delayMs: number
    • valOrFunc: T

    Returns Promise<T>

  • each<T>(array: readonly T[], func: Consumer<T>): Promise<void>
  • async version of array.forEach

    • iterate through array and await call func with each element and index

    Sample:

    await xaa.each([1, 2, 3], async val => await xaa.delay(val))
    

    Type parameters

    • T

    Parameters

    • array: readonly T[]

      array to each

    • func: Consumer<T>

      callback for each

    Returns Promise<void>

  • filter<T>(array: readonly T[], func: Predicate<T>): Promise<any[]>
  • async filter array

    Sample:

    await xaa.filter([1, 2, 3], async val => await validateResult(val))
    

    Beware: concurrency is fixed to 1.

    Type parameters

    • T

    Parameters

    • array: readonly T[]

      array to filter

    • func: Predicate<T>

      callback for filter

    Returns Promise<any[]>

    filtered result

  • makeDefer<T>(Promise?: PromiseConstructor): Defer<T>
  • Create a promise Defer object

    Sample:

    async function waitEvent() {
    const defer = xaa.makeDefer();
    someThing.on("event", (data) => defer.resolve(data))
    return defer.promise;
    }

    Type parameters

    • T

    Parameters

    • Promise: PromiseConstructor = global.Promise

      optional Promise constructor.

    Returns Defer<T>

    Defer instance

  • async map array with concurrency

    • intended to be similar to bluebird.map

    Type parameters

    • T

    • O

    Parameters

    • array: readonly T[]

      array to map, if any item is promise-like, it will be resolved first.

    • Optional func: MapFunction<T, O>

      callback to map values from the array

    • options: MapOptions = ...

      MapOptions

    Returns Promise<O[]>

    promise with mapped result

  • runTimeout<T>(tasks: Task<T>, maxMs: number, rejectMsg?: string): Promise<T>
  • runTimeout<T>(tasks: Tasks<T>, maxMs: number, rejectMsg?: string): Promise<T[]>
  • Calls timeout(maxMs, rejectMsg).run(tasks)

    Type parameters

    • T

    Parameters

    • tasks: Task<T>

      Promise or function or array of them

    • maxMs: number

      milliseconds to wait for the tasks to fulfill

    • Optional rejectMsg: string

      message to reject with if operation timed out

    Returns Promise<T>

    promise results from all tasks

  • Type parameters

    • T: readonly any[]

    Parameters

    • tasks: Tasks<T>
    • maxMs: number
    • Optional rejectMsg: string

    Returns Promise<T[]>

  • Create a TimeoutRunner to run tasks (promises) with timeout

    Sample:

    await xaa.timeout(1000, "timeout fetching data").run(() => fetch(url))
    

    with promises:

    await xaa.timout(1000).run([promise1, promise2])
    

    Type parameters

    • T

    Parameters

    • maxMs: number

      number of milliseconds to allow tasks to run

    • rejectMsg: string = "xaa TimeoutRunner operation timed out"

      message to reject with when timeout triggers

    Returns TimeoutRunner<T>

    TimeoutRunner object

  • tryCatch<T, TAlt>(funcOrPromise: Producer<T>, valOrFunc?: ValueOrErrorHandler<TAlt>): Promise<T | TAlt>
  • try to:

    • await a promise
    • call and await function that returns a promise

    If exception occur, then return valOrFunc

    • if valOrFunc is a function, then return valOrFunc(err)

    Type parameters

    • T

    • TAlt

    Parameters

    • funcOrPromise: Producer<T>

      function or promise to try

    • Optional valOrFunc: ValueOrErrorHandler<TAlt>

      value, or callback to get value, to return if func throws

    Returns Promise<T | TAlt>

    result, valOrFunc, or valOrFunc(err).

  • wrap<T, F>(func: F, ...args2: Parameters<F>): Promise<T>
  • Wrap the calling of a function into async/await (promise) context

    • intended to be similar to bluebird.try

    Type parameters

    • T

    • F: (...args: any[]) => T

    Parameters

    • func: F

      function to wrap in async context

    • Rest ...args2: Parameters<F>

    Returns Promise<T>

    result from func

Generated using TypeDoc