Created
March 12, 2019 05:54
-
-
Save evilsoft/92f43cbd66bd368d2a82cde96fe1b871 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Async, identity } from 'crocks' | |
// A Promise is not a Functor, since it is not | |
// a functor it cannot be a Monad | |
Promise | |
.resolve(Promise.resolve(3)) | |
.then(console.log) | |
//=> 3 | |
// should return a Promise that will return 3 | |
// nestedAsync :: Async e (Async e Number) | |
const nestedAsync = | |
Async.of(Async.of(3)) | |
// Async is a Functor and a Monad, it returns | |
// the inner nested Async. | |
nestedAsync.fork( | |
console.log.bind(console, 'rejected:'), | |
console.log.bind(console, 'resolved:') | |
) | |
//=> logs "resolved: Async Function" | |
// Async is a Functor and a Monad, it can also | |
// join (the heart of the Monoid portion of a Monad) | |
// by chaining `identity` | |
nestedAsync | |
.chain(identity) | |
.fork( | |
console.log.bind(console, 'rejected:'), | |
console.log.bind(console, 'resolved:') | |
) | |
//=> logs resolved: 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment