, 13 tweets, 4 min read Read on Twitter
Welcome to twelfth episode of #monadicmonday! Today I want to talk about a Future – a concept of asynchronous lazy task, which is a far better replacement for an eager Promise.
Due to their laziness, futures support cancellation, racing, parallelism and resource safety out of the box. The best library I've seen and used for Futures is fluture-js: github.com/fluture-js/Flu….
Fun fact: in Scala world `Future` is not a monad, but something akin to JS's `Promise` – i.e., eager referentially non-transparent construct. Instead, it's far better to use IO (cats), Task (monix & scalaz) or ZIO (zio) to achieve the described benefits of today's topic.
In `fp-ts` & StaticLand terminology, Future<T> could be described as Task<Either<Error, T>>. But there's much more to it than my simplification implies. So let's dive in!
You have quite a bunch of ways to create a Future. For full list of method go see github.com/fluture-js/Flu…
Please note that due to TS's way of type inference I have to provide type arguments manually:
Why Future is bright and cool? Well, first of all, it is a pure value – i.e., the Future will not be running until you call `fork` or `promise` methods:
Second, Futures support cancellation, making timing out requests a breeze:
Third, Futures are stack-safe:
Fourth, Futures are monads, bifunctors and alternatives, so you can use all the machinery you've used to:
Finally, `fluture` has a nice touch – so kind of imitation for Haskell's `do` notation, available by names `do` and `go`. As a side note, I must admit that attempts to write types for its results are quite cumbersome, but TS can infer them flawlessly:
So as you can see, Futures are a very powerful functional concept, and working with them is really easy thanks to the awesome `fluture-js`! I encourage you to read its documentation and examples, and try it in your projects – it's battle-tested and highly performant.
The documentation for the latest production release is available at github.com/fluture-js/Flu…
This concludes my short introduction to programming with Futures. As usual, all code examples are available at github.com/YBogomolov/mon…
Missing some Tweet in this thread?
You can try to force a refresh.

Like this thread? Get email updates or save it to PDF!

Subscribe to Yuriy Bogomolov
Profile picture

Get real-time email alerts when new unrolls are available from this author!

This content may be removed anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just three indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!