Skip to content

An Effect monad library. Allows you to defer dealing with impurities until you actually run them.

Notifications You must be signed in to change notification settings

johnpaulada/side-effector

Repository files navigation

SideEffector

An Effect monad library. Allows you to defer dealing with impurities until you actually run them.

forthebadge forthebadge

Quickstart

Importing the library

Import via require

const SideEffector = require('side-effector')

Import as ES module

import SideEffector from 'side-effector'

Import in the browser

<script src="https://cdn.jsdelivr.net/npm/side-effector@0.1.1/side-effector.min.js"></script>

Using the library

To create a SideEffector, give it a function.

Example:

const lazyZero = SideEffector(() => 0)

Apply a function using map()

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.

Example:
const toAddOne = lazyZero.map(x => x + 1)
const toDisplayPlusOne = toAddOne.map(x => console.log(x + 1))

Run the side effects using run()

To actually run the side effects, we use the run() method like this:

Example:
toDisplayPlusOne.run() // 2

Notes

Derived from James Sinclair's post, How to Deal with Dirty Side-Effects in Your Pure Functional JavaScript.

License

MIT

About

An Effect monad library. Allows you to defer dealing with impurities until you actually run them.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published