Await Expressions

Available on .NET only, the await expression construct can be used to "unwrap" otherwise asynchronous code, so that future results can be dealt with in a linear fashion.

Under the hood, await will break the method into different parts, scheduling them to be executed asynchronously once the awaited actions have been completed.

method Test: Task<String>;
begin
  var task := new Task<String>(-> begin
      Thread.Sleep(10000);
      exit 'Result!'
    end);
  task.Start();
  exit task;
end;

...

method OtherMethod();
begin
  Console.WriteLine(await Test);
  Console.WriteLine('after test');
end;

At the point of the await in OtherMethod, the actual method containing the await will return, retuning the future.

If a method using await returns a type, it must be a Task<T>.

 

.NET Only

  • await is currently available on the .NET platform only.

See Also