loading...

re: Explain Futures and Streams in Dart like I'm Five VIEW POST

FULL DISCUSSION
 

By Dart documentation:

A Future represents a computation that doesn’t complete immediately. Where a normal function returns the result, an asynchronous function returns a Future, which will eventually contain the result. The future will tell you when the result is ready.

and

A stream is a sequence of asynchronous events. It is like an asynchronous Iterable where, instead of getting the next event when you ask for it, the stream tells you that there is an event when it is ready.

I find that sometimes real-world analogies work well for explaining / remembering concepts. Here's one - it's not perfect but it helps me.

Think that you are at one of those modern sushi restaurants where you have a belt going around the room with sushi boats on it. You just sit down and wait till one goes by, grab it and eat. But they also allow you to order carry out.

  • A Future is like the token with a number on it that they give you when you order takeout; you made the request, but the result is not yet ready but you have a placeholder. And when the result is ready, you get a callback (the digital board above the takeout counter shows your number or they shout it out) - you can now go in and grab your food (the result) to take out.

  • A Stream is like that belt carrying little sushi bowls. By sitting down at that table, you've "subscribed" to the stream. You don't know when the next sushi boat will arrive - but when the chef (message source) places it in the stream (belt), then the subscribers will receive it. The important thing to note is that they arrive asynchronously (you have no idea when the next boat/message will come) but they will arrive in sequence (i.e., if the chef puts three types of sushi on the belt, in some order -- you will see them come by you in that same order)

From a coding perspective -- both Futures and Streams help you deal with asynchrony (where things don't happen instantly, and you don't know when you will get a result after you make a request).

The difference is that Futures are about one-shot request/response (I ask, there is a delay, I get a notification that my Future is ready to collect, and I'm done!) whereas Streams are a continuous series of responses to a single request (I ask, there is a delay, then I keep getting responses until the stream dries up or I decide to close it and walk away).

Hope that helps.

 

This really clarified things for me thanks a ton!

 

Best explanation ever. I now fully understand. Thanks

 

I had read the docs but was still unsure about the concepts. Your analogies have shed some light on the topic. Especially on streams.

The difference is that Futures are about one-shot request/response (I ask, there is a delay, I get a notification that my Future is ready to collect, and I'm done!) whereas Streams are a continuous series of responses to a single request (I ask, there is a delay, then I keep getting responses until the stream dries up or I decide to close it and walk away).

This answers the questions I've asked in my previous two replies above. So, thanks. Knowing this I can now explore creating streams as tests since it somewhat makes sense now.

Code of Conduct Report abuse