

Alternative, you can insert some pauses in your calculation which makes space for the Dart VM to execute other events on the event queue. If you are going to use it for heavy calculations, you can either spawn an Isolate so your main isolate instance can wait on the other isolate. timeout is kind of pointless but what it really are for is when you are making some IO operations like API requests where the Dart VM are actually waiting for some answer. So from the Future's point of view, you are returning a result before the deadline. This Timer event are going on top on the event queue like any other event in the Dart VM.īut in your first example, we are actually never going to execute any other event on the event queue before you are giving the result. timeout actually does, is creating an internal Timer which are going to be triggered in the future unless you get a result before the timeout value. So if you are running a CPU intensive calculation without any pauses you are stopping the Dart VM from running any other events on the event queue. timeout needs to run in the same single thread and Dart can't just stop the execution of your own code.
#Microsoft dart alternative code
This behavior can be confusing but it is important to remember that your Dart code are only executed in a single thread (unless you are using isolates). I'm assuming I messed up something in the longComputation, but what? There were no un-awaited Futures. Int startTime = DateTime.now().millisecondsSinceEpoch įor(int i = 1 i longComputation() async For this problem I found Future.timeout, it would almost do the same thing that I want, except it doesn't work for this code. If it doesn't finish in a specific time, abort the computation and return a different value. I want to run a computation that might take too long.
