How I can await Tasks from async / await functions?

Arron Washington 3 years ago updated by Lazlo Bonin (Lead Developer) 2 years ago 1

How I can await Tasks from async / await functions?

Consider this method signature:

public async Task DoAction() { }

Bolt doesn't seem to have a built in way to naturally await DoAction.

I have tried the "Wait Until" unit, checking Task.IsCompleted, (see screenshot) but that just causes the async function to be executed over and over and over again. 

What's the right way to handle async / await functions in Bolt?

Bolt Version:
Unity Version:
Scripting Backend:
.NET Version (API Compatibility Level):

Hi Arron,

Unfortunately, Bolt does not yet have support for async/await semantics introduced in C# 6.

The Wait Until method you posted seems like a good workaround though. The reason your method gets called again and again is because the scoped return value of the task (that output port) does not exist in the scope of the wait until delegate.

What you could do is: 

  1. After DoActorAI, set a graph variable (called e.g. "Task") with the result of your task
  2. After that Set Variable unit, call Wait Until like you do right now
  3. Instead of connecting the output of DoActorAI to IsCompleted, connect the output of Set Variable.