Skip to content

Instantly share code, notes, and snippets.

@wuct
Last active November 3, 2018 20:18
Show Gist options
  • Save wuct/6964a9b426c625b2bb774ef4cd9c7f12 to your computer and use it in GitHub Desktop.
Save wuct/6964a9b426c625b2bb774ef4cd9c7f12 to your computer and use it in GitHub Desktop.
Let JavaScript Promise be a functor
Promise.prototype.fmap = Promise.prototype.then
Promise.resolve('yolo')
.fmap(str => str + ' swag!')
.then(console.log) // "yolo swag!"
@modern-defensive
Copy link

promise.then(x => g(f(x))) is NOT equivalent to promise.then(f).then(g)
see
https://stackoverflow.com/questions/45712106/why-are-promises-monads

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment