Node.js utils to deal with async/await.
Installation
npm install --save mono-utils
Usage
INFO: You need node
>= 8.0.0
to use mono-utils since it uses native async/await
const ok cb waitFor ... =
Utils
ok
Waits for the value of the Promise
and returns its value. If the promise throws an Error
, returns undefined
.
: Promise
Example:
const ok = const readFile = // readFile sends back a Promise since we use fs-extraconst file = await if file console
cb
Calls a function Node style function as first argument function (err, result)
, all the others arguments will be given to the function. Waits for the callback result, throws an Error
if err
is truthy.
: Promise
Example:
const ok = const fs = try const file = await catch err // Could not read file
waitFor
Waits for ms
milliseconds to pass, use setTimeout
under the hood.
: Promise
Example:
const waitFor = await // wait for 1s
waitForEvent
Waits for emitter to emit an eventName event.
: Promise<Array>
Example:
const waitFor = await
asyncObject
Waits for all Promises in the keys of obj to resolve.
: Promise<Object>
Example:
const asyncObject = const pictures comments tweets = await console
asyncMap
Waits for all Promises mapped by fn
:
: Promise<Array>
Example:
const asyncMap = const posts = await
asyncForEach
Loop for every item in array and call fn
and wait for it to finish in series:
: Promise<void>
Example:
const asyncMap = const posts = await
Other utils
We developed other utils that you might find useful: