How to make an asynchronous Dart call synchronous? How to make an asynchronous Dart call synchronous? dart dart

How to make an asynchronous Dart call synchronous?


The only time that you can wrap an async method in a synchronous one is when you don't need to get a return value.

For example if you want to disable the save button, save results to the server asynchronously and re-enable the save button when the job is done you can write it like this:

Future<bool> save() async {  // save changes async here  return true;}void saveClicked() {  saveButton.enabled = false;  save()    .then((success) => window.alert(success ? 'Saved' : 'Failed'))    .catchError((e) => window.alert(e))    .whenComplete(() { saveButton.enabled = true; });}

Note that the saveClicked method is fully synchronous, but executes the save method asynchronously.

Note that if you make saveClicked async, not only do you have to call it using the async pattern, but the entire method body will run asynchronously so the save button will not be disabled when the function returns.

For completeness the async version of saveClicked looks like this:

Future<Null> saveClicked() async {  saveButton.enabled = false;  try {    bool success = await save();    window.alert(success ? 'Saved' : 'Failed');  }  catch (e) {    window.alert(e);  }  finally {    saveButton.enabled = true;  }}


Yes, this is way late, but I think this is a cool feature new people should know about.

There is a way, but the Dart docs warn against it (and it's somehow "experimental", although the implications aren't really discussed).

The waitFor command.

You basically pass in an asynchronous function that returns a Future, an optional timeout parameter, and the waitFor function will return the result.

For example:

final int number = waitFor<int>(someAsyncThatReturnsInt);


The resync function cannot be implemented in Dart's current execution model.

Asynchronous execution is contagious. A synchronous function must return before any other asynchronous events can execute, so there is no way to synchronously wait for asynchronous execution.

Execution in Dart is single-threaded and event based. There is no way for the resync function to block without it also blocking all other execution in the same isolate, so the pending async operations will never happen.

To block the synchronous execution, and continue executing something else, you need to preserve the entire call stack up to that point, and reinstate it later when the synchronous operations have completed. If you have that functionality, then there are probably better ways to do things than Future and Stream :)

Also, waiting for "all async execution" isn't well-defined in an event based system. There might be a broadcast Stream emitting events coming in from the network, a periodic timer, or a receive port getting data from another isolate, or some other source of events that you can't wait for because they come from outside the isolate, or event the process. When the current isolate shuts down, it might send a final shut-down message to another isolate, so effectively the "async execution" isn't over until the isolate dies.

Using the async/await syntax, you won't get synchronous operation, but it will be easier to code the similar asynchronous operation:

function() async {  var result = await asyncFunction();  return result;}

It won't wait for async operations that aren't reflected in the Future returned by asyncFunction, but that's the job of asyncFunction to not complete until its operations are complete.