on-error
Generate error-first callback handlers naturally.
Handle errors via a dedicated error handler function, or by emitting them. Optionally invoke a 2nd function on no error, or regardless of error.
Potentially reduce the number of branches needing tests.
examples
var onError = // error handler {} // just do something on error// or emit it // if doSomething invokes this generated callback with an error// handleIt will be called with that error, otherwise our // anonymous function will be called with all remaining arguments // if error handleIt, and always... // if error handleIt, and always (include error for always func)... // maybe we want to emit the error instead...var emitter = // or, *gasp*, chain to a promise
api
var onError =
onError(cb)
Returns a function that when called with a truthy first argument, will execute cb
with said argument.
onError.emit(emitter)
Returns a function that when called with a truthy first argument, will emit error
on the provided event emitter
with said argument.
onError(cb1).otherwise(cb2)
Returns a function that when called with a truthy first argument, will execute cb1
with said argument. When executed with a non-truthy 1st argument, cb2
will instead be executed with the error argument stripped.
.otherwise
may also be chained from onError.emit()
onError(cb1).otherwiseWithError(cb2)
Returns a function that when called with a truthy first argument, will execute cb1
with said argument. When executed with a non-truthy 1st argument, cb2
will instead be executed with the error argument in-tact.
.otherwiseWithError
may also be chained from onError.emit()
onError(cb1).always(cb2)
Returns a function that when called with a truthy first argument, will execute cb1
with said argument. In addition, cb2
will always be executed with the error argument stripped.
.always
may also be chained from onError.emit()
onError(cb1).alwaysWithError(cb2)
Returns a function that when called with a truthy first argument, will execute cb1
with said argument. In addition, cb2
will always be executed with the error argument in-tact.
.alwaysWithError
may also be chained from onError.emit()
testing
npm test [--dot | --spec] [--grep=pattern]
Specifying --dot
or --spec
will change the output from the default TAP style.
Specifying --grep
will only run the test files that match the given pattern.
coverage
npm run coverage [--html]
This will output a textual coverage report. Including --html
will also open an HTML coverage report in the default browser.