UNPKG

@barchart/common-js

Version:
74 lines (73 loc) 2.55 kB
/** * Utilities for working with promises. * * @public * @module lang/promise */ /** * Creates a composite promise which resolves normally or rejects is a specified * amount of time elapses. * * @public * @static * @async * @param {Promise} promise * @param {number} milliseconds * @param {string=} description * @returns {Promise<*>} */ export function timeout(promise: Promise<any>, milliseconds: number, description?: string | undefined): Promise<any>; /** * A mapping function that works asynchronously. Given an array of items, each item through * a mapping function, which can return a promise. Then, this function returns a single promise * which is the result of each mapped promise. * * @public * @static * @async * @param {Array} items - The items to map * @param {Function} mapper - The mapping function (e.g. given an item, return a promise). * @param {number=} concurrency - The maximum number of promises that are allowed to run at once. * @returns {Promise<Array>} */ export function map(items: any[], mapper: Function, concurrency?: number | undefined): Promise<any[]>; /** * Runs a series of functions sequentially (where each function can be * synchronous or asynchronous). The result of each function is passed * to the successive function and the result of the final function is * returned to the consumer. * * @static * @public * @async * @param {Function[]} functions - An array of functions, each expecting a single argument. * @param {*=} input - The argument to pass the first function. * @returns {Promise<*>} */ export function pipeline(functions: Function[], input?: any | undefined): Promise<any>; /** * Given an array of functions, where each returns a promise, runs * the functions in sequential order, until one of the function * returns a successful promise with a non-null result. Any * rejected promise is ignored. * * @public * @async * @param {Function[]} executors * @returns {Promise} */ export function first(executors: Function[]): Promise<any>; /** * Creates a new promise, given an executor. * * This is a wrapper for the {@link Promise} constructor; however, any error * is caught and the resulting promise is rejected (instead of letting the * error bubble up to the top-level handler). * * @public * @static * @async * @param {Function} executor - A function which has two callback parameters. The first is used to resolve the promise, the second rejects it. * @returns {Promise} */ export function build(executor: Function): Promise<any>;