Syncronous waiting for a Future or a Stream to complete in Dart
dart
Solution
For future visitors coming here simply wanting to perform some task after a Future or Stream completes, use `await` and `await for` inside an async method.
Future
final myInt = await getFutureInt();
Stream
int mySum = 0;
await for (int someInt in myIntStream) {
mySum += someInt;
}
Note
This may be technically different than performing a synchronous task, but it achieves the goal of completing one task before doing another one.
Problem
I'm playing with a tiny web server and I'm implementing one version using the `async` package, and one synchronous version executing each request in a separate isolate. I would like to simply pipe a file stream to the `HttpResponse`, but I can't do that synchronously. And I can't find a way to wait for neither the `Stream` nor a `Future` synchronously. I'm now using a `RandomAccessFile` instead which works, but it becomes messier. One solution would be to execute a periodical timer to check if the future is completed (by setting a boolean or similar), but that is most definitely not something I want to use. Is there a way to wait synchronously for a `Future` and a `Stream`? If not, why?