Do not return Future in #dart, it's not safe β οΈ
Wait what! What should I do instead?
There is an alternative, and it's easier and safer ππΌββοΈ
It's called Task, let's learn about it ππ§΅
Conversation
Why not Future?
π Impure: the result will change at every request
π Not composable: not easy to work with a sequence of Future
π Error handling: not easy to handle and spot errors
2
1
4
What is Task? π€
Task comes from functional programming
It makes async requests pure, composable, and error friendly
How is that? π
1
5
How can Task make the function pure? π€¨
Simple: Task doesn't make the request, yet
Task is a thunk: a function that, when called, will return a Future π‘
2
9
Task is simply a *description* of your request
It doesn't do anything until run() is called ππΌββοΈ
1
3
Task is included in fpdart
You can now chain methods to extract and validate the response, without actually making any request πͺ
Replying to
You can see an example of using Task (TaskEither) in this post π
1
10
Note π‘
Future is not wrong and it works great π€
Task is just a wrapper around Future after all ππΌββοΈ
The point is that Task brings extra features and a safer API (Functional programming π)
Quote Tweet
Replying to @SandroMaglione
I find your tag-line a bit misleading.
I think most of us have been using futures just fine.
I get that you want to promote your package and it does sound like an improvement, but saying Futures should not be returned might confuse new users.
3
1
8
π
Quote Tweet
Well, this was an overstatement
Future *is* safe and works great in #dart
To redeem myself, I am writing a complete article that aims to clarify Future & Task and make a comparison
Comment below any question that you would like me to answer
twitter.com/SandroMaglioneβ¦
2

