Sometimes it is useful to wait for completion of one or more background executions. This can be required if the caller needs to behave synchronously, or if multiple asynchronous tasks have been started in parallel.
Use the await action to wait for a task to complete. In this variation of the basic process (see Create a callback), an await step is added just after the call to block the task until the call is complete.
[
{
"name": "anchor",
"script": "stepTypeCallback",
"config": {},
"next": {
"prepare": "call",
"*": false
}
},
{
"name": "call",
"script": "stepTypeCall",
"config": {
"callAction": "run",
"background": true,
"callback": true
},
"next": "await"
},
{
"name": "await",
"script": "stepTypeCallback",
"config": {
"callbackAction": "await"
},
"next": {
"complete": "complete",
"*": false
}
},
{
"name": "complete",
"script": "stepTypeSet",
"config": {
"show": false,
"createEvent": true,
"description": "Complete"
},
"next": false
},
{
"name": "run",
"script": "stepTypeRunScript",
"config": {
"script": "application.sleep(10);"
},
"next": false
}
]
Under the covers, await works just like query, but continues in a loop until the process is in an inactive state (complete, timeout, cancel, error). It uses the same merging logic as query, returning any saved data along with the createData from the prepare step. Await also checks the timeout timestamp each time, and will force a timeout status if the timeout is reached.
Use the config option callbackPollInterval to set the interval at which the callback is checked. This is an ISO 8601 duration. The default is PT10S, i.e. every ten seconds. The interval should be between 2 seconds and 5 minutes, and will be adjusted if not.
Remember that there is an overall hard limit for wait time (typically 30 minutes). If your await exceeds this, your process will terminate, often without returning. If your await is likely to be this long, redesign to be more fully asynchronous.
With await, the original anchor step can still define a ‘complete’ branch, which will execute once the background task completes, before control returns from await.