nonsynchronous
async/await callback fusioning utilities
Functions
Promisify
Implementation:
const promisify =
The same promisify as provided in Node core.
Example:
const promisify = const fs = const stat = { console}
Once
Implementation:
const once =
The same once as provided in Node core (also polyfilled for earlier Node 10 versions).
Example:
const once = const http = { const server = http server await console}
When
Useful for when you want to explicitly use a callback based API within an async function. The function returned from when
must be called after awaiting the done
method on that function
other wise an error called before awaiting done()
will be thrown.
Implementation:
const when = { var { throw Error'called before awaiting done()' } const fn = fndone = return fn}
Example:
const when = { const until = await until console}
Whenify
Useful for using a callback based API within an async function without any modifications required within the callback itself.
Must be used with a callback last API (for callback first, just use when
manually).
Used in conjunction with the done
symbol and optionally
the count
symbol. The asyncOps
option is explored in the count
symbol documentation.
Implementation:
const whenify = { const until = const max = asyncOps - 1 const result = { const cb = args return } resultcount = 0 Object return result}
Example:
const whenify done = const fs = { const stat = await statdone}
Whenify Method
Whenify an object method so that method[done]
can be awaited (where done
is the done
symbol). This can be particularly useful when writing tests for an instance with callback based methods. The test be both self documenting and async compatible.
Implementation:
const whenifyMethod = { const result = instancemethod = result return instance}
Example:
const whenifyMethod done = const fs = { fs await fsstatdone}
Promisify Method
Convert a callback-based method on an instance into its promisifed equivalent. Can be passed multiple method names to easily promisify entire instances.
Implementation:
const _promisifyMethod = { const result = instancemethod = result return instance}const promisifyMethod = { methods return instance}
Example:
const promisifyMethod = const http = { const server = http await server console}
Promisify Of
Partially apply a promisify of a particular method on any given instance
without mutating the instance. This is useful as an alternative to promisifyMethod
if you don't want to overwrite instance keys and/or
you have multiple different instances that you want to promisify the same
method of.
Implementation:
const promisifyOf = { return }
Example:
const promisifyOf = const http = { const server = http const listen = await 2000 // listen on port 2000 console}
Immediate
Promisified setImmediate
.
Implementation:
const immediate =
Example:
const immediate = { console await console}
Timeout
Promisified setTimeout
.
Implementation:
const timeout =
Example:
const timeout = { await console}
Symbols
Done
The done
symbol is used with whenify
and whenifyMethod
to access a promise on the function that has been whenified
which can be awaited. The promise will complete once the
callback for a whenified function has been called.
Count
The count
symbol is used with whenify
and whenifyMethod
to access a the total amount
of times a whenified functions callback was called. This is
only useful when you set the asyncOps
option to greater
than 1.
const whenify done count = const multiCallbackThing = { }
Custom Promisify Args
The customPromisifyArgs
symbol exposes a Node core symbol used
to alter promisify
behaviour for callbacks then
have extra arguments.
const promisify customPromisifyArgs = const multiArgApi = multiArgApicustomPromisifyArgs = 'shape' 'color'const multiValApi = { const shape color = await console}
Behaviors
Automatically includes and enable make-promisies-safe which causes Node to treat unhandled rejections as unhandled exceptions (e.g. throws and exits).
Tests & Coverage
npm test
Suites: 1 passed, 1 of 1 completed
Asserts: 24 passed, of 24
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.js | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
License
MIT