An Effect monad library. Allows you to defer dealing with impurities until you actually run them.
const SideEffector = require('side-effector')
import SideEffector from 'side-effector'
<script src="https://cdn.jsdelivr.net/npm/side-effector@0.1.1/side-effector.min.js"></script>
To create a SideEffector, give it a function.
const lazyZero = SideEffector(() => 0)
To apply a function to this object, we use the map()
method.
It's important to remember that the function will not be immediately applied.
const toAddOne = lazyZero.map(x => x + 1)
const toDisplayPlusOne = toAddOne.map(x => console.log(x + 1))
To actually run the side effects, we use the run()
method like this:
toDisplayPlusOne.run() // 2
Derived from James Sinclair's post, How to Deal with Dirty Side-Effects in Your Pure Functional JavaScript.
MIT