async

Asynchronous tools.

License:
  • Apache-2.0
Source:

Members

(static) createTimedBarrier

PromisePool internal interface.

Source:

Methods

(inner) ap(f)

Time monad - ap.

Source:
Parameters:
Name Type Description
f

f: a -> b

Returns:

mf: ma -> mb

(inner) bind(ma, f)

Time monad - >>=.

Source:
Parameters:
Name Type Description
ma
f

f: a -> mb

Returns:

(ma >>= f): mb

(inner) cancellable(p) → {Object}

Make any promise cancellable.

Example:

let { promise, cancel } = async.cancellable(
    async.timeout(() => "Job done!", 2000)
)

promise.then(utils.to_("resolved")).catch(utils.to_("rejected"))

cancel("I've changed my mind")
Source:
Parameters:
Name Type Description
p Promise
Returns:
Type:
Object

{ promise, cancel, resolve }

(inner) createMutex() → {Object}

Mutual exclusion for asynchronous functions.

Example:

const mutex = async.createMutex()

let f = async m => {
    let val = await m.lock()
    return `Freed with val: ${val}`
}

f(mutex).then(utils.to_("success")).catch(utils.to_("failure"))

mutex.resolve(42)  //  mutex.reject("ERROR")
Source:
Returns:
Type:
Object

lock(), resolve(), reject()

(inner) createTimedBarrier(releaseTimeoutopt) → {Object}

Create mutex with watchdog (10 minutes by default).

barrier, in contrast to mutex, is preventing node process from exiting.

Example:

const barrier = createBarrier<void>();

setTimeout(() => { barrier.resolve("Released!"); }, 2000);

await barrier.lock();
Source:
Parameters:
Name Type Attributes Default Description
releaseTimeout <optional>
10*timeUnit.minute
Returns:
Type:
Object

lock(), resolve(), reject()

(async, inner) delay(timeopt, passCancelopt) → {Promise.<Number>}

Delay current async execution by time miliseconds.

Example:

(async () => {
    await async.delay()
    console.log("Hello ...")
    await async.delay()
    console.log("... world")
})()
Source:
See:
Parameters:
Name Type Attributes Default Description
time Number <optional>
timeUnit.second
passCancel function <optional>

cancel => any

Returns:
Type:
Promise.<Number>

(async, inner) interval(f, passClearopt, timeopt) → {Promise.<any>}

setInterval in Promise / async skin.

Example 1:

global.x = 0
async.interval(
    c => {
        if (global.x === 10) { c("tada") }
        else { global.x += 1 }
        console.log("ping", Date.now())
    }
)
.then(utils.to_("success"))
.catch(utils.to_("failure"))

Example 2:

interval(
    () => { console.log("Hey!"); return 42 },
    c => timeout(() => c(), 4 * timeUnit.second)
)
.then(x => console.log("Finished:", x))
.catch(c => console.log("Error:", c))
Source:
Parameters:
Name Type Attributes Default Description
f function
passClear function <optional>

(Function) => any

time Number <optional>
timeUnit.second
Returns:
Type:
Promise.<any>

(inner) liftr(f)

Time monad - liftr.

Source:
Parameters:
Name Type Description
f

f: a[] -> b

Returns:

f: a[] -> mb

(async, inner) map(arr, f) → {Promise.<Array>}

Asynchronous version of standard Array.prototype.map function.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • element - currently processed element
    • index - current index

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.map(
        array.range(10),
        x => async.timeout(() => 4*x, 100*x)
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Description
arr Array
f function
Returns:
Type:
Promise.<Array>

(async, inner) parMap(arr, f) → {Promise.<Array>}

Asynchronous version of standard Array.prototype.map function.

Implementation that does paralell execution.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • element - currently processed element
    • index - current index

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.parMap(
        array.range(10),
        x => async.timeout(() => 4*x, 100*x)
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Description
arr Array
f function
Returns:
Type:
Promise.<Array>

(inner) promisePool(poolSizeopt) → {Object}

Imperative promise pool.

const pool = promisePool(128);
for (element of dataSeries) {
    let r = await pool.exec(async () => {
        // ...
    });
    if (r.status === "fulfilled") {
        // ...
    } else if (r.status === "rejected") {
        // ...
    }
}
let rest = await pool.finish();
Source:
Parameters:
Name Type Attributes Default Description
poolSize <optional>
64
Returns:
Type:
Object

exec(t), finish()

(async, inner) race(…ps) → {Promise}

Resolve or reject when any of the promises passed as arguments resolve or reject.

Mirror of the standard function Promise.race().

Example:

m1 = async.createMutex()
m2 = async.createMutex()

async.race(m1.lock(), m2.lock())
    .then(utils.to_("resolved"))
    .catch(utils.to_("rejected"))

m1.resolve("All right!")  //  or, e.g: m2.reject("Some left!")
Source:
Parameters:
Name Type Attributes Description
ps Promise <repeatable>

promises

Returns:
Type:
Promise

(inner) rbind(f, ma)

Time monad - =<<.

Source:
Parameters:
Name Type Description
f

f: a -> mb

ma
Returns:

(f =<< ma): mb

(async, inner) reduce(arr, f, initAccopt) → {Promise.<unknown>}

Asynchronous version of standard Array.prototype.reduce function.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • acc - accumulates the f's return values; it is the accumulated value previously returned in the last invocation of f, or initAcc, if supplied.
    • element - currently processed element
    • index - current index
  • initAcc - value to use as the first argument to the first call of the f. If no initial value is supplied, the first element in the array will be used.

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.reduce(
        array.range(10),
        (acc, x) => async.timeout(() => acc+x, 100*x),
        0
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Attributes Description
arr Array
f function
initAcc unknown <optional>
Returns:
Type:
Promise.<unknown>

(async, inner) repeat(f, condition) → {Promise.<any>}

Repeat f (sync. or async.) while condition evaluates to true.

Resolves with result of last f execution when condition evaluates to false.

Source:
Parameters:
Name Type Description
f function
condition function
Returns:
Type:
Promise.<any>

(async, inner) timeout(f, timeopt, passCancelopt) → {Promise.<any>}

setTimeout in Promise / async skin.

Example:

async.timeout(
    () => { console.log("Hey!"); return 42 }, 2000,
    c => async.timeout(() => c("Cancelled!"), 1000)
)
.then(x => console.log("Success:", x))
.catch(c => console.log("Error or cancel:", c))
Source:
Parameters:
Name Type Attributes Default Description
f function
time Number <optional>
timeUnit.second
passCancel function <optional>

cancel => any

Returns:
Type:
Promise.<any>

(inner) unit(val)

Time monad - return.

Source:
Parameters:
Name Type Description
val
Returns:

promise

Asynchronous tools.

License:
  • Apache-2.0
Source:

Members

(static) createTimedBarrier

PromisePool internal interface.

Source:

Methods

(inner) ap(f)

Time monad - ap.

Source:
Parameters:
Name Type Description
f

f: a -> b

Returns:

mf: ma -> mb

(inner) bind(ma, f)

Time monad - >>=.

Source:
Parameters:
Name Type Description
ma
f

f: a -> mb

Returns:

(ma >>= f): mb

(inner) cancellable(p) → {Object}

Make any promise cancellable.

Example:

let { promise, cancel } = async.cancellable(
    async.timeout(() => "Job done!", 2000)
)

promise.then(utils.to_("resolved")).catch(utils.to_("rejected"))

cancel("I've changed my mind")
Source:
Parameters:
Name Type Description
p Promise
Returns:
Type:
Object

{ promise, cancel, resolve }

(inner) createMutex() → {Object}

Mutual exclusion for asynchronous functions.

Example:

const mutex = async.createMutex()

let f = async m => {
    let val = await m.lock()
    return `Freed with val: ${val}`
}

f(mutex).then(utils.to_("success")).catch(utils.to_("failure"))

mutex.resolve(42)  //  mutex.reject("ERROR")
Source:
Returns:
Type:
Object

lock(), resolve(), reject()

(inner) createTimedBarrier(releaseTimeoutopt) → {Object}

Create mutex with watchdog (10 minutes by default).

barrier, in contrast to mutex, is preventing node process from exiting.

Example:

const barrier = createBarrier<void>();

setTimeout(() => { barrier.resolve("Released!"); }, 2000);

await barrier.lock();
Source:
Parameters:
Name Type Attributes Default Description
releaseTimeout <optional>
10*timeUnit.minute
Returns:
Type:
Object

lock(), resolve(), reject()

(async, inner) delay(timeopt, passCancelopt) → {Promise.<Number>}

Delay current async execution by time miliseconds.

Example:

(async () => {
    await async.delay()
    console.log("Hello ...")
    await async.delay()
    console.log("... world")
})()
Source:
See:
Parameters:
Name Type Attributes Default Description
time Number <optional>
timeUnit.second
passCancel function <optional>

cancel => any

Returns:
Type:
Promise.<Number>

(async, inner) interval(f, passClearopt, timeopt) → {Promise.<any>}

setInterval in Promise / async skin.

Example 1:

global.x = 0
async.interval(
    c => {
        if (global.x === 10) { c("tada") }
        else { global.x += 1 }
        console.log("ping", Date.now())
    }
)
.then(utils.to_("success"))
.catch(utils.to_("failure"))

Example 2:

interval(
    () => { console.log("Hey!"); return 42 },
    c => timeout(() => c(), 4 * timeUnit.second)
)
.then(x => console.log("Finished:", x))
.catch(c => console.log("Error:", c))
Source:
Parameters:
Name Type Attributes Default Description
f function
passClear function <optional>

(Function) => any

time Number <optional>
timeUnit.second
Returns:
Type:
Promise.<any>

(inner) liftr(f)

Time monad - liftr.

Source:
Parameters:
Name Type Description
f

f: a[] -> b

Returns:

f: a[] -> mb

(async, inner) map(arr, f) → {Promise.<Array>}

Asynchronous version of standard Array.prototype.map function.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • element - currently processed element
    • index - current index

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.map(
        array.range(10),
        x => async.timeout(() => 4*x, 100*x)
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Description
arr Array
f function
Returns:
Type:
Promise.<Array>

(async, inner) parMap(arr, f) → {Promise.<Array>}

Asynchronous version of standard Array.prototype.map function.

Implementation that does paralell execution.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • element - currently processed element
    • index - current index

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.parMap(
        array.range(10),
        x => async.timeout(() => 4*x, 100*x)
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Description
arr Array
f function
Returns:
Type:
Promise.<Array>

(inner) promisePool(poolSizeopt) → {Object}

Imperative promise pool.

const pool = promisePool(128);
for (element of dataSeries) {
    let r = await pool.exec(async () => {
        // ...
    });
    if (r.status === "fulfilled") {
        // ...
    } else if (r.status === "rejected") {
        // ...
    }
}
let rest = await pool.finish();
Source:
Parameters:
Name Type Attributes Default Description
poolSize <optional>
64
Returns:
Type:
Object

exec(t), finish()

(async, inner) race(…ps) → {Promise}

Resolve or reject when any of the promises passed as arguments resolve or reject.

Mirror of the standard function Promise.race().

Example:

m1 = async.createMutex()
m2 = async.createMutex()

async.race(m1.lock(), m2.lock())
    .then(utils.to_("resolved"))
    .catch(utils.to_("rejected"))

m1.resolve("All right!")  //  or, e.g: m2.reject("Some left!")
Source:
Parameters:
Name Type Attributes Description
ps Promise <repeatable>

promises

Returns:
Type:
Promise

(inner) rbind(f, ma)

Time monad - =<<.

Source:
Parameters:
Name Type Description
f

f: a -> mb

ma
Returns:

(f =<< ma): mb

(async, inner) reduce(arr, f, initAccopt) → {Promise.<unknown>}

Asynchronous version of standard Array.prototype.reduce function.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • acc - accumulates the f's return values; it is the accumulated value previously returned in the last invocation of f, or initAcc, if supplied.
    • element - currently processed element
    • index - current index
  • initAcc - value to use as the first argument to the first call of the f. If no initial value is supplied, the first element in the array will be used.

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.reduce(
        array.range(10),
        (acc, x) => async.timeout(() => acc+x, 100*x),
        0
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Attributes Description
arr Array
f function
initAcc unknown <optional>
Returns:
Type:
Promise.<unknown>

(async, inner) repeat(f, condition) → {Promise.<any>}

Repeat f (sync. or async.) while condition evaluates to true.

Resolves with result of last f execution when condition evaluates to false.

Source:
Parameters:
Name Type Description
f function
condition function
Returns:
Type:
Promise.<any>

(async, inner) timeout(f, timeopt, passCancelopt) → {Promise.<any>}

setTimeout in Promise / async skin.

Example:

async.timeout(
    () => { console.log("Hey!"); return 42 }, 2000,
    c => async.timeout(() => c("Cancelled!"), 1000)
)
.then(x => console.log("Success:", x))
.catch(c => console.log("Error or cancel:", c))
Source:
Parameters:
Name Type Attributes Default Description
f function
time Number <optional>
timeUnit.second
passCancel function <optional>

cancel => any

Returns:
Type:
Promise.<any>

(inner) unit(val)

Time monad - return.

Source:
Parameters:
Name Type Description
val
Returns:

promise

Asynchronous tools.

License:
  • Apache-2.0
Source:

Members

(static) createTimedBarrier

PromisePool internal interface.

Source:

Methods

(inner) ap(f)

Time monad - ap.

Source:
Parameters:
Name Type Description
f

f: a -> b

Returns:

mf: ma -> mb

(inner) bind(ma, f)

Time monad - >>=.

Source:
Parameters:
Name Type Description
ma
f

f: a -> mb

Returns:

(ma >>= f): mb

(inner) cancellable(p) → {Object}

Make any promise cancellable.

Example:

let { promise, cancel } = async.cancellable(
    async.timeout(() => "Job done!", 2000)
)

promise.then(utils.to_("resolved")).catch(utils.to_("rejected"))

cancel("I've changed my mind")
Source:
Parameters:
Name Type Description
p Promise
Returns:
Type:
Object

{ promise, cancel, resolve }

(inner) createMutex() → {Object}

Mutual exclusion for asynchronous functions.

Example:

const mutex = async.createMutex()

let f = async m => {
    let val = await m.lock()
    return `Freed with val: ${val}`
}

f(mutex).then(utils.to_("success")).catch(utils.to_("failure"))

mutex.resolve(42)  //  mutex.reject("ERROR")
Source:
Returns:
Type:
Object

lock(), resolve(), reject()

(inner) createTimedBarrier(releaseTimeoutopt) → {Object}

Create mutex with watchdog (10 minutes by default).

barrier, in contrast to mutex, is preventing node process from exiting.

Example:

const barrier = createBarrier<void>();

setTimeout(() => { barrier.resolve("Released!"); }, 2000);

await barrier.lock();
Source:
Parameters:
Name Type Attributes Default Description
releaseTimeout <optional>
10*timeUnit.minute
Returns:
Type:
Object

lock(), resolve(), reject()

(async, inner) delay(timeopt, passCancelopt) → {Promise.<Number>}

Delay current async execution by time miliseconds.

Example:

(async () => {
    await async.delay()
    console.log("Hello ...")
    await async.delay()
    console.log("... world")
})()
Source:
See:
Parameters:
Name Type Attributes Default Description
time Number <optional>
timeUnit.second
passCancel function <optional>

cancel => any

Returns:
Type:
Promise.<Number>

(async, inner) interval(f, passClearopt, timeopt) → {Promise.<any>}

setInterval in Promise / async skin.

Example 1:

global.x = 0
async.interval(
    c => {
        if (global.x === 10) { c("tada") }
        else { global.x += 1 }
        console.log("ping", Date.now())
    }
)
.then(utils.to_("success"))
.catch(utils.to_("failure"))

Example 2:

interval(
    () => { console.log("Hey!"); return 42 },
    c => timeout(() => c(), 4 * timeUnit.second)
)
.then(x => console.log("Finished:", x))
.catch(c => console.log("Error:", c))
Source:
Parameters:
Name Type Attributes Default Description
f function
passClear function <optional>

(Function) => any

time Number <optional>
timeUnit.second
Returns:
Type:
Promise.<any>

(inner) liftr(f)

Time monad - liftr.

Source:
Parameters:
Name Type Description
f

f: a[] -> b

Returns:

f: a[] -> mb

(async, inner) map(arr, f) → {Promise.<Array>}

Asynchronous version of standard Array.prototype.map function.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • element - currently processed element
    • index - current index

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.map(
        array.range(10),
        x => async.timeout(() => 4*x, 100*x)
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Description
arr Array
f function
Returns:
Type:
Promise.<Array>

(async, inner) parMap(arr, f) → {Promise.<Array>}

Asynchronous version of standard Array.prototype.map function.

Implementation that does paralell execution.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • element - currently processed element
    • index - current index

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.parMap(
        array.range(10),
        x => async.timeout(() => 4*x, 100*x)
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Description
arr Array
f function
Returns:
Type:
Promise.<Array>

(inner) promisePool(poolSizeopt) → {Object}

Imperative promise pool.

const pool = promisePool(128);
for (element of dataSeries) {
    let r = await pool.exec(async () => {
        // ...
    });
    if (r.status === "fulfilled") {
        // ...
    } else if (r.status === "rejected") {
        // ...
    }
}
let rest = await pool.finish();
Source:
Parameters:
Name Type Attributes Default Description
poolSize <optional>
64
Returns:
Type:
Object

exec(t), finish()

(async, inner) race(…ps) → {Promise}

Resolve or reject when any of the promises passed as arguments resolve or reject.

Mirror of the standard function Promise.race().

Example:

m1 = async.createMutex()
m2 = async.createMutex()

async.race(m1.lock(), m2.lock())
    .then(utils.to_("resolved"))
    .catch(utils.to_("rejected"))

m1.resolve("All right!")  //  or, e.g: m2.reject("Some left!")
Source:
Parameters:
Name Type Attributes Description
ps Promise <repeatable>

promises

Returns:
Type:
Promise

(inner) rbind(f, ma)

Time monad - =<<.

Source:
Parameters:
Name Type Description
f

f: a -> mb

ma
Returns:

(f =<< ma): mb

(async, inner) reduce(arr, f, initAccopt) → {Promise.<unknown>}

Asynchronous version of standard Array.prototype.reduce function.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • acc - accumulates the f's return values; it is the accumulated value previously returned in the last invocation of f, or initAcc, if supplied.
    • element - currently processed element
    • index - current index
  • initAcc - value to use as the first argument to the first call of the f. If no initial value is supplied, the first element in the array will be used.

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.reduce(
        array.range(10),
        (acc, x) => async.timeout(() => acc+x, 100*x),
        0
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Attributes Description
arr Array
f function
initAcc unknown <optional>
Returns:
Type:
Promise.<unknown>

(async, inner) repeat(f, condition) → {Promise.<any>}

Repeat f (sync. or async.) while condition evaluates to true.

Resolves with result of last f execution when condition evaluates to false.

Source:
Parameters:
Name Type Description
f function
condition function
Returns:
Type:
Promise.<any>

(async, inner) timeout(f, timeopt, passCancelopt) → {Promise.<any>}

setTimeout in Promise / async skin.

Example:

async.timeout(
    () => { console.log("Hey!"); return 42 }, 2000,
    c => async.timeout(() => c("Cancelled!"), 1000)
)
.then(x => console.log("Success:", x))
.catch(c => console.log("Error or cancel:", c))
Source:
Parameters:
Name Type Attributes Default Description
f function
time Number <optional>
timeUnit.second
passCancel function <optional>

cancel => any

Returns:
Type:
Promise.<any>

(inner) unit(val)

Time monad - return.

Source:
Parameters:
Name Type Description
val
Returns:

promise

Asynchronous tools.

License:
  • Apache-2.0
Source:

Members

(static) createTimedBarrier

PromisePool internal interface.

Source:

Methods

(inner) ap(f)

Time monad - ap.

Source:
Parameters:
Name Type Description
f

f: a -> b

Returns:

mf: ma -> mb

(inner) bind(ma, f)

Time monad - >>=.

Source:
Parameters:
Name Type Description
ma
f

f: a -> mb

Returns:

(ma >>= f): mb

(inner) cancellable(p) → {Object}

Make any promise cancellable.

Example:

let { promise, cancel } = async.cancellable(
    async.timeout(() => "Job done!", 2000)
)

promise.then(utils.to_("resolved")).catch(utils.to_("rejected"))

cancel("I've changed my mind")
Source:
Parameters:
Name Type Description
p Promise
Returns:
Type:
Object

{ promise, cancel, resolve }

(inner) createMutex() → {Object}

Mutual exclusion for asynchronous functions.

Example:

const mutex = async.createMutex()

let f = async m => {
    let val = await m.lock()
    return `Freed with val: ${val}`
}

f(mutex).then(utils.to_("success")).catch(utils.to_("failure"))

mutex.resolve(42)  //  mutex.reject("ERROR")
Source:
Returns:
Type:
Object

lock(), resolve(), reject()

(inner) createTimedBarrier(releaseTimeoutopt) → {Object}

Create mutex with watchdog (10 minutes by default).

barrier, in contrast to mutex, is preventing node process from exiting.

Example:

const barrier = createBarrier<void>();

setTimeout(() => { barrier.resolve("Released!"); }, 2000);

await barrier.lock();
Source:
Parameters:
Name Type Attributes Default Description
releaseTimeout <optional>
10*timeUnit.minute
Returns:
Type:
Object

lock(), resolve(), reject()

(async, inner) delay(timeopt, passCancelopt) → {Promise.<Number>}

Delay current async execution by time miliseconds.

Example:

(async () => {
    await async.delay()
    console.log("Hello ...")
    await async.delay()
    console.log("... world")
})()
Source:
See:
Parameters:
Name Type Attributes Default Description
time Number <optional>
timeUnit.second
passCancel function <optional>

cancel => any

Returns:
Type:
Promise.<Number>

(async, inner) interval(f, passClearopt, timeopt) → {Promise.<any>}

setInterval in Promise / async skin.

Example 1:

global.x = 0
async.interval(
    c => {
        if (global.x === 10) { c("tada") }
        else { global.x += 1 }
        console.log("ping", Date.now())
    }
)
.then(utils.to_("success"))
.catch(utils.to_("failure"))

Example 2:

interval(
    () => { console.log("Hey!"); return 42 },
    c => timeout(() => c(), 4 * timeUnit.second)
)
.then(x => console.log("Finished:", x))
.catch(c => console.log("Error:", c))
Source:
Parameters:
Name Type Attributes Default Description
f function
passClear function <optional>

(Function) => any

time Number <optional>
timeUnit.second
Returns:
Type:
Promise.<any>

(inner) liftr(f)

Time monad - liftr.

Source:
Parameters:
Name Type Description
f

f: a[] -> b

Returns:

f: a[] -> mb

(async, inner) map(arr, f) → {Promise.<Array>}

Asynchronous version of standard Array.prototype.map function.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • element - currently processed element
    • index - current index

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.map(
        array.range(10),
        x => async.timeout(() => 4*x, 100*x)
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Description
arr Array
f function
Returns:
Type:
Promise.<Array>

(async, inner) parMap(arr, f) → {Promise.<Array>}

Asynchronous version of standard Array.prototype.map function.

Implementation that does paralell execution.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • element - currently processed element
    • index - current index

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.parMap(
        array.range(10),
        x => async.timeout(() => 4*x, 100*x)
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Description
arr Array
f function
Returns:
Type:
Promise.<Array>

(inner) promisePool(poolSizeopt) → {Object}

Imperative promise pool.

const pool = promisePool(128);
for (element of dataSeries) {
    let r = await pool.exec(async () => {
        // ...
    });
    if (r.status === "fulfilled") {
        // ...
    } else if (r.status === "rejected") {
        // ...
    }
}
let rest = await pool.finish();
Source:
Parameters:
Name Type Attributes Default Description
poolSize <optional>
64
Returns:
Type:
Object

exec(t), finish()

(async, inner) race(…ps) → {Promise}

Resolve or reject when any of the promises passed as arguments resolve or reject.

Mirror of the standard function Promise.race().

Example:

m1 = async.createMutex()
m2 = async.createMutex()

async.race(m1.lock(), m2.lock())
    .then(utils.to_("resolved"))
    .catch(utils.to_("rejected"))

m1.resolve("All right!")  //  or, e.g: m2.reject("Some left!")
Source:
Parameters:
Name Type Attributes Description
ps Promise <repeatable>

promises

Returns:
Type:
Promise

(inner) rbind(f, ma)

Time monad - =<<.

Source:
Parameters:
Name Type Description
f

f: a -> mb

ma
Returns:

(f =<< ma): mb

(async, inner) reduce(arr, f, initAccopt) → {Promise.<unknown>}

Asynchronous version of standard Array.prototype.reduce function.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • acc - accumulates the f's return values; it is the accumulated value previously returned in the last invocation of f, or initAcc, if supplied.
    • element - currently processed element
    • index - current index
  • initAcc - value to use as the first argument to the first call of the f. If no initial value is supplied, the first element in the array will be used.

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.reduce(
        array.range(10),
        (acc, x) => async.timeout(() => acc+x, 100*x),
        0
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Attributes Description
arr Array
f function
initAcc unknown <optional>
Returns:
Type:
Promise.<unknown>

(async, inner) repeat(f, condition) → {Promise.<any>}

Repeat f (sync. or async.) while condition evaluates to true.

Resolves with result of last f execution when condition evaluates to false.

Source:
Parameters:
Name Type Description
f function
condition function
Returns:
Type:
Promise.<any>

(async, inner) timeout(f, timeopt, passCancelopt) → {Promise.<any>}

setTimeout in Promise / async skin.

Example:

async.timeout(
    () => { console.log("Hey!"); return 42 }, 2000,
    c => async.timeout(() => c("Cancelled!"), 1000)
)
.then(x => console.log("Success:", x))
.catch(c => console.log("Error or cancel:", c))
Source:
Parameters:
Name Type Attributes Default Description
f function
time Number <optional>
timeUnit.second
passCancel function <optional>

cancel => any

Returns:
Type:
Promise.<any>

(inner) unit(val)

Time monad - return.

Source:
Parameters:
Name Type Description
val
Returns:

promise

Asynchronous tools.

License:
  • Apache-2.0
Source:

Members

(static) createTimedBarrier

PromisePool internal interface.

Source:

Methods

(inner) ap(f)

Time monad - ap.

Source:
Parameters:
Name Type Description
f

f: a -> b

Returns:

mf: ma -> mb

(inner) bind(ma, f)

Time monad - >>=.

Source:
Parameters:
Name Type Description
ma
f

f: a -> mb

Returns:

(ma >>= f): mb

(inner) cancellable(p) → {Object}

Make any promise cancellable.

Example:

let { promise, cancel } = async.cancellable(
    async.timeout(() => "Job done!", 2000)
)

promise.then(utils.to_("resolved")).catch(utils.to_("rejected"))

cancel("I've changed my mind")
Source:
Parameters:
Name Type Description
p Promise
Returns:
Type:
Object

{ promise, cancel, resolve }

(inner) createMutex() → {Object}

Mutual exclusion for asynchronous functions.

Example:

const mutex = async.createMutex()

let f = async m => {
    let val = await m.lock()
    return `Freed with val: ${val}`
}

f(mutex).then(utils.to_("success")).catch(utils.to_("failure"))

mutex.resolve(42)  //  mutex.reject("ERROR")
Source:
Returns:
Type:
Object

lock(), resolve(), reject()

(inner) createTimedBarrier(releaseTimeoutopt) → {Object}

Create mutex with watchdog (10 minutes by default).

barrier, in contrast to mutex, is preventing node process from exiting.

Example:

const barrier = createBarrier<void>();

setTimeout(() => { barrier.resolve("Released!"); }, 2000);

await barrier.lock();
Source:
Parameters:
Name Type Attributes Default Description
releaseTimeout <optional>
10*timeUnit.minute
Returns:
Type:
Object

lock(), resolve(), reject()

(async, inner) delay(timeopt, passCancelopt) → {Promise.<Number>}

Delay current async execution by time miliseconds.

Example:

(async () => {
    await async.delay()
    console.log("Hello ...")
    await async.delay()
    console.log("... world")
})()
Source:
See:
Parameters:
Name Type Attributes Default Description
time Number <optional>
timeUnit.second
passCancel function <optional>

cancel => any

Returns:
Type:
Promise.<Number>

(async, inner) interval(f, passClearopt, timeopt) → {Promise.<any>}

setInterval in Promise / async skin.

Example 1:

global.x = 0
async.interval(
    c => {
        if (global.x === 10) { c("tada") }
        else { global.x += 1 }
        console.log("ping", Date.now())
    }
)
.then(utils.to_("success"))
.catch(utils.to_("failure"))

Example 2:

interval(
    () => { console.log("Hey!"); return 42 },
    c => timeout(() => c(), 4 * timeUnit.second)
)
.then(x => console.log("Finished:", x))
.catch(c => console.log("Error:", c))
Source:
Parameters:
Name Type Attributes Default Description
f function
passClear function <optional>

(Function) => any

time Number <optional>
timeUnit.second
Returns:
Type:
Promise.<any>

(inner) liftr(f)

Time monad - liftr.

Source:
Parameters:
Name Type Description
f

f: a[] -> b

Returns:

f: a[] -> mb

(async, inner) map(arr, f) → {Promise.<Array>}

Asynchronous version of standard Array.prototype.map function.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • element - currently processed element
    • index - current index

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.map(
        array.range(10),
        x => async.timeout(() => 4*x, 100*x)
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Description
arr Array
f function
Returns:
Type:
Promise.<Array>

(async, inner) parMap(arr, f) → {Promise.<Array>}

Asynchronous version of standard Array.prototype.map function.

Implementation that does paralell execution.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • element - currently processed element
    • index - current index

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.parMap(
        array.range(10),
        x => async.timeout(() => 4*x, 100*x)
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Description
arr Array
f function
Returns:
Type:
Promise.<Array>

(inner) promisePool(poolSizeopt) → {Object}

Imperative promise pool.

const pool = promisePool(128);
for (element of dataSeries) {
    let r = await pool.exec(async () => {
        // ...
    });
    if (r.status === "fulfilled") {
        // ...
    } else if (r.status === "rejected") {
        // ...
    }
}
let rest = await pool.finish();
Source:
Parameters:
Name Type Attributes Default Description
poolSize <optional>
64
Returns:
Type:
Object

exec(t), finish()

(async, inner) race(…ps) → {Promise}

Resolve or reject when any of the promises passed as arguments resolve or reject.

Mirror of the standard function Promise.race().

Example:

m1 = async.createMutex()
m2 = async.createMutex()

async.race(m1.lock(), m2.lock())
    .then(utils.to_("resolved"))
    .catch(utils.to_("rejected"))

m1.resolve("All right!")  //  or, e.g: m2.reject("Some left!")
Source:
Parameters:
Name Type Attributes Description
ps Promise <repeatable>

promises

Returns:
Type:
Promise

(inner) rbind(f, ma)

Time monad - =<<.

Source:
Parameters:
Name Type Description
f

f: a -> mb

ma
Returns:

(f =<< ma): mb

(async, inner) reduce(arr, f, initAccopt) → {Promise.<unknown>}

Asynchronous version of standard Array.prototype.reduce function.

  • arr - array to operate on
  • f - async or sync function with signature:
    • this - bound to arr
    • acc - accumulates the f's return values; it is the accumulated value previously returned in the last invocation of f, or initAcc, if supplied.
    • element - currently processed element
    • index - current index
  • initAcc - value to use as the first argument to the first call of the f. If no initial value is supplied, the first element in the array will be used.

f can return Promise.<unknown> or <unknown>

Example:

(async () => {
    let x = await async.reduce(
        array.range(10),
        (acc, x) => async.timeout(() => acc+x, 100*x),
        0
    )
    console.log(x)
})()
Source:
Parameters:
Name Type Attributes Description
arr Array
f function
initAcc unknown <optional>
Returns:
Type:
Promise.<unknown>

(async, inner) repeat(f, condition) → {Promise.<any>}

Repeat f (sync. or async.) while condition evaluates to true.

Resolves with result of last f execution when condition evaluates to false.

Source:
Parameters:
Name Type Description
f function
condition function
Returns:
Type:
Promise.<any>

(async, inner) timeout(f, timeopt, passCancelopt) → {Promise.<any>}

setTimeout in Promise / async skin.

Example:

async.timeout(
    () => { console.log("Hey!"); return 42 }, 2000,
    c => async.timeout(() => c("Cancelled!"), 1000)
)
.then(x => console.log("Success:", x))
.catch(c => console.log("Error or cancel:", c))
Source:
Parameters:
Name Type Attributes Default Description
f function
time Number <optional>
timeUnit.second
passCancel function <optional>

cancel => any

Returns:
Type:
Promise.<any>

(inner) unit(val)

Time monad - return.

Source:
Parameters:
Name Type Description
val
Returns:

promise