Vachan

Vachan

Promise utility functions

Constructor

new Vachan()

Source:

Methods

(static) debounce(fn, delay, optionsopt)

Source:
See:

Returns an async function that delays calling fn
until after wait milliseconds have elapsed since the last time it was called

Parameters:
Name Type Attributes Description
fn function

function to debounce

delay number

ms to wait before calling fn.

options object <optional>

object of {leading, fixed}

Properties
Name Type Description
leading: boolean

(default false)
Call the fn on the leading edge of the timeout.
Meaning immediately, instead of waiting for wait milliseconds.

fixed: boolean

fixed delay, each call won't reset the timer to 0

(static) exec(promise, optionsopt)

Source:

Execute a promise / function, without caring about its results

Parameters:
Name Type Attributes Description
promise Promise.<*> | function
options object <optional>

(static) exit(promise, optionsopt)

Source:

Execute a promise / function, and exit when it completes

Parameters:
Name Type Attributes Description
promise Promise.<*> | function
options object <optional>

(static) finally(promise, onFinally) → {Promise.<*>}

Source:
See:

Promise.finally polyfill
Invoked when the promise is settled regardless of outcome

Parameters:
Name Type Description
promise Promise.<*>
onFinally function
Returns:

Returns a Promise.

Type
Promise.<*>

(static) identity(promise) → {Promise.<*>}

Source:

identity function is to make sure returned value is a promise.
returns the following if the input is a:

  • promise: returns the promise itself
  • function: executes the function and returns the result wrapped in a promise
  • any: returns the input wrapped in a promise
Parameters:
Name Type Description
promise function | Promise.<*> | any
Returns:
Type
Promise.<*>

(static) lazy(executor) → {Promise.<*>}

Source:

create a lazy promise from an executor function ((resolve, reject) => {})
a lazy promise defers execution till .then() or .catch() is called

Parameters:
Name Type Description
executor function

function(resolve, reject) {},
same as promise constructor

Returns:

a lazy promise

Type
Promise.<*>

(static) lazyFrom()

Source:

create a lazy promise from an async function
a lazy promise defers execution till .then() or .catch() is called

(static) promiseMap(iterable, mapper, options) → {Promise.<Array.<any>>}

Source:

Returns a Promise that is fulfilled when all promises in input
and ones returned from mapper are fulfilled, or rejects if any
of the promises reject. The fulfilled value is an Array of the
fulfilled values returned from mapper in input order.

Parameters:
Name Type Description
iterable array | object | Map.<any, any> | Set.<*>

collection to iterate over

mapper function

The function invoked per iteration, should return a promise
mapper is invoked with (value, index|key, iterable)

options object

object of {concurrency}
concurrency: Number of maximum concurrently running promises, default is Infinity

Returns:

a promise that resolves to an array of results

Type
Promise.<Array.<any>>

(static) promiseMapKeys(iterable, mapper, options) → {Promise.<Array.<any>>}

Source:

Like promiseMap but for keys

Parameters:
Name Type Description
iterable array | object | Map.<any, any> | Set.<*>
mapper function
options object
Returns:

a promise that resolves to an array of results

Type
Promise.<Array.<any>>

(static) promiseMapValues(iterable, mapper, options) → {Promise.<Array.<any>>}

Source:

Like promiseMap but for values

Parameters:
Name Type Description
iterable array | object | Map.<any, any> | Set.<*>
mapper function
options object
Returns:

a promise that resolves to an array of results

Type
Promise.<Array.<any>>

(static) sleep(duration)

Source:

Returns a promise that resolves after the specified duration
Can be used to delay / sleep
Example: await Vachan.sleep(2000);

Parameters:
Name Type Description
duration number

milliseconds to delay for

(static) timeout(promise, optionsopt)

Source:

Returns a promise the rejects on specified timeout

Parameters:
Name Type Attributes Description
promise Promise.<*> | function

A Promise or an async function

options object | number <optional>

can be {timeout} or a number
timeout: Milliseconds before timing out