always-done
Handle completion and errors with elegance! Support for streams, callbacks, promises, child processes, async/await and sync functions. A drop-in replacement for async-done - pass 100% of its tests plus more
Table of Contents
Install
Install with npm
$ npm i always-done --save
Usage
For more use-cases see the tests
const fs = const alwaysDone =
Background
Behind the scenes we use just good plain old try/catch
block. Sounds you strange? See what "hard" job is done on try-catch-callback and try-catch-core.
In the first one, we just calls a function inside try/catch and calls done
callback with error or result of that function.
About second one, there we wraps the done
callback with once and dezalgo to ensure it will be called in the next tick.
Here, in always-done
, we just give a callback
to that try-catch-core package and "listen" what is the result. Actually we not listening anything, we just make a few checks to understand what the incoming value is - promise, child process, stream, observable and etc.
Resolution
Nothing so magical. Try/catch block for most of the things works briliant. And on-stream-end module (which is drop-in replacement for end-of-stream) for streams and child processes.
API
alwaysDone
Handle completion of
fn
and optionally passdone
callback, otherwise it returns a thunk. If thatthunk
does not accept function, it returns another thunk until you passdone
to it.
Params
<fn>
{Function}: function to be called[opts]
{Object}: optional options, such ascontext
andargs
, passed to try-catch-core[opts.context]
{Object}: context to be passed tofn
[opts.args]
{Array}: custom argument(s) to be pass tofn
, given value is arrayified[opts.passCallback]
{Boolean}: passtrue
if you wantcb
to be passed tofn
args[done]
{Function}: on completionreturns
{Function}: thunk until you passdone
to that thunk
Example
var alwaysDone = var options = context: num: 123 bool: true args:
Supports
Handles completion and errors of async/await, synchronous and asynchronous (callback) functions, also functions that returns streams, promises, child process and observables.
async/await
completion
Handles
Callbacks completion
var alwaysDone =
Completion of synchronous functions
Returning a value
Returning an error
Completion of Promises
Returning a resolved Promise
Returning a rejected Promise
Streams completion
Using on-stream-end and stream-exhaust
Unpiped streams
Failing unpiped streams
Failing piped streams
Handles completion of Observables
Using
.subscribe
method of the observable
Empty observable
var Observable = Observable
Successful observable
Failing observable
Completion of Child Process
Basically, they are streams, so completion is handled using on-stream-end which is drop-in replacement for end-of-stream
Successful exec
var cp =
Failing exec
var cp =
Failing spawn
var cp =
Handling errors
uncaught exceptions
thrown errors
Always completes
It may looks strange, but it's logical. If you pass empty function it just completes with
undefined
result andnull
error.
Example
// passing empty function
Passing custom context
var alwaysDone = var opts = context: foo: 'bar'
Passing custom arguments
It may be strange, but this allows you to pass more arguments to that first function and the last argument always will be "callback" until
fn
is async or sync but withpassCallback: true
option.
var alwaysDone = var options = args: 1 2
Returning a thunk
Can be used as thunkify lib without problems, just don't pass a done callback.
var fs = var alwaysDone = var readFileThunk =
Related
- async-done: Force async using nextTick and normalize completion/errors for callbacks, promises, observables, child… more | homepage
- base: base is the foundation for creating modular, unit testable and highly pluggable… more | homepage
- end-of-stream: Call a callback when a readable/writable/duplex stream has completed or failed. | homepage
- is-node-stream: Strictly and correctly checks if value is a nodejs stream. | homepage
- minibase: MiniBase is minimalist approach to Base - @node-base, the awesome framework. Foundation… more | homepage
- on-stream-end: Handles completion and errors of any stream - readable/writable/duplex. | homepage
- try-catch-callback: try/catch block with a callback, used in try-catch-core. Use it when you… more | homepage
- try-catch-core: Low-level package to handle completion and errors of sync or asynchronous functions… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
But before doing anything, please read the CONTRIBUTING.md guidelines.