0

Does Dart have anything remotely similar to the async/await pattern in .net?

For example, I want to open a socket and do several handshakes: send 1, wait for response 1, send 2, wait for response 2 etc. Waiting for a response shouldn't be blocking obviously, that's what async is all about.

Here's one way of doing it:

Socket _socket;

Socket.connect(_host, _port).then(
  (Socket socket) {
      print("socket open!");
      _socket = socket;
      socket.transform(new StringDecoder()).listen(ProcessResponse);
      socket.write("1");
  });


void ProcessResponse(String response)
{
  print("response received!");
  if (response == "1") _socket.write("2");
  if (response == "2") _socket.write("3");
  if (response == "3") _socket.write("4");
  // ... etc ..
}

I'd love to be able to write something like

socket.write("1");
response1 = await socket.getResponse();
socket.write("2");
response2 = await socket.getResponse();
socket.write("3");
response3 = await socket.getResponse();
// etc.

i.e. write code that looks sync and is much easier to understand, but actually runs async.

Any ideas?

Florian Loitsch
  • 7,698
  • 25
  • 30
Max
  • 9,220
  • 10
  • 51
  • 83

1 Answers1

2

No, not yet. This is a pretty widely desired feature, though. See https://code.google.com/p/dart/issues/detail?id=104 (and star the issue).

Now it has: https://www.dartlang.org/articles/await-async/

Conchylicultor
  • 4,631
  • 2
  • 37
  • 40
Shailen Tuli
  • 13,815
  • 5
  • 40
  • 51