How to make a synchronous asynchronous dart call?

I am going to evaluate Dart for a German company by porting various Java programs to Dart, comparing and analyzing the results. In the browser, Dart wins. For the performance of the server software, it seemed like a serious question (see this question about me ), but basically it was neutralized.

Now I am moving some "simple" command line tools where I did not expect serious problems, but at least one. Some of these tools make HTTP requests to collect some data, and the standalone Dart virtual machine only supports them asynchronously. Looking through everything I could find, it is not possible to use any asynchronous call in the main synchronous software.

I understand that I could reorganize the available synchronous software into asynchronous. But that would turn a well-designed piece of software into something less readable and harder to debug and maintain. For some pieces of software, this just doesn't make sense. My question is: is there a way (overlooked) to embed an asynchronous call in a synchronously called method?

I assume that it will not be difficult to provide a system call that can only be used from the main thread, which simply transfers execution to the entire list of asynchronous calls in the queue (without having to complete the main thread first) and as soon as the last one has received execution, it returns and continues the main one flow.

Something that might look like this:

var synchFunction() {
  var result;
  asyncFunction().then(() { result = ...; });

  resync(); // the system call to move to and wait out all async execution

  return result;
}

Having such a method would also simplify the lib APIs. Most “synchronous” calls can be deleted since the resynchronization call will complete the task. This seems to be such a logical idea that I still think it exists, and I missed it. Or is there a good reason why this will not work?


After thinking about the answer received from lm(see below) for two days, I still do not understand why the encapsulation of an asynchronous call of Dart to synchronous should not be possible. This is done in the "normal" synchronous programming world all the time. Typically, you can wait for resynchronization by getting “Finish” from the asynchronous procedure, or if something fails to continue after the timeout.

With this in mind, my first sentence can be strengthened as follows:

var synchFunction() {
  var result;
  asyncFunction()
    .then(() { result = ...; })
    .whenComplete(() { continueResync() }); // the "Done" message

  resync(timeout); // waiting with a timeout as maximum limit

  // Either we arrive here with the [result] filled in or a with a [TimeoutException].
  return result;
}

resync() , main , ( , ). continueResync(), , , resync() . timeout continueResync(), resync() TimeoutException.

, ( , ), , .

, lm . - "" , : -, Dart?

+7
4

, , - .

, , , , :

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; });
}

, saveClicked , save .

, saveClicked async, async, , , .

saveClicked :

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;
  }
}
+6

resync .

. , , .

Dart . resync , , async .

- , , . , , , -, :)

, " " . , , , - , , . , , " " , .

async/wait, , :

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

, Future, asyncFunction, asyncFunction .

+2

. . API, , dart:io, , , , /, .

async/await async , ( ).

, . , .

+1

Yes, it's too late, but I think this is a great opportunity that new people should know about.

There is a way, but Dart Docs warn about this (and it is somehow "experimental", although the consequences are not actually discussed).

Team . waitFor

Essentially, you are passing an asynchronous function that returns an Futureoptional time parameter timeout, and the function waitForreturns the result.

For instance:

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

All Articles