run-io
A monadic abstraction to encapsulate functions with side-effects. The single target of this super-tiny library is to ease testing of side-effect based code.
Installation
$ npm i run-io
API
lift(fn, [opts])
Turns fn
into a yield
able function. opts
is optional and can have the following keys:
sync
- Handles this function synchronously
liftMethod(object, method, [opts])
Turns a object method into a yield
able function. opts
is the same as in lift()
.
Example
;
run(generatorFunction, [done])
Generates a function, which can be called with arguments that get passed to the generatorFunction
.
If called this function executes the effects, yield
ed by the generator function.
If you omit the done
parameter, run
will assume that you pass the callback as the last parameter
to the result function.
Example
{ const send = ; ;} { if err throw err; console;} http;
Usage
const io = ; const lift = iolift;const liftMethod = ioliftMethod;const run = iorun; const getUser = { // Stuff ...}; const getUserArticles = { // Stuff ...}; /** * Some http handler which performs heavy IO. */ { const user = ; const send = ; let articles; try articles = user_id; catch e ; return; ;} /** * A smoke test for the handler. Notice how we don't even need an * implementation of the `getUser()` and `getUserArticles()` methods. */ { const req = {}; const res = {} ; // eslint-disable-line // Just fun to test. No database setup needed. const user = _id: 3 ; const articles = title: 'foo' ; const send = ; const it = ; t; t; t;} ; // Run the handler.http; { // Error handling ...}