The await() and awaitWithTimeout() functions pause workflow execution until a specified condition becomes true. These are commonly used to wait for signals or state changes.
await()
Pauses workflow execution until the provided condition returns true.
Signature
function await($condition): PromiseInterface
Parameters
A callable that returns a boolean. The workflow will pause until this returns true
Returns
PromiseInterface
React\Promise\PromiseInterface
A promise that resolves when the condition becomes true
Usage
use Workflow\Workflow;
use Workflow\SignalMethod;
use function Workflow\await;
class ApprovalWorkflow extends Workflow
{
private bool $approved = false;
#[SignalMethod]
public function approve(): void
{
$this->approved = true;
}
public function execute()
{
// Wait until approved
yield await(fn () => $this->approved);
return 'approved';
}
}
awaitWithTimeout()
Pauses workflow execution until the provided condition returns true or the timeout is reached.
Signature
function awaitWithTimeout(int|string|CarbonInterval $seconds, $condition): PromiseInterface
Parameters
seconds
int|string|CarbonInterval
The timeout duration in seconds, a string duration (e.g., “5 minutes”), or a CarbonInterval
A callable that returns a boolean. The workflow will pause until this returns true or timeout is reached
Returns
PromiseInterface
React\Promise\PromiseInterface
A promise that resolves when the condition becomes true or the timeout expires
Usage
use Workflow\Workflow;
use Workflow\SignalMethod;
use function Workflow\awaitWithTimeout;
class TimedApprovalWorkflow extends Workflow
{
private bool $approved = false;
#[SignalMethod]
public function approve(): void
{
$this->approved = true;
}
public function execute()
{
// Wait up to 60 seconds for approval
yield awaitWithTimeout(60, fn () => $this->approved);
if ($this->approved) {
return 'approved';
}
return 'timeout';
}
}
Example with Multiple Conditions
use Workflow\Workflow;
use Workflow\SignalMethod;
use function Workflow\await;
class MultiConditionWorkflow extends Workflow
{
private bool $conditionA = false;
private bool $conditionB = false;
private bool $canceled = false;
#[SignalMethod]
public function setConditionA(): void
{
$this->conditionA = true;
}
#[SignalMethod]
public function setConditionB(): void
{
$this->conditionB = true;
}
#[SignalMethod]
public function cancel(): void
{
$this->canceled = true;
}
public function execute()
{
// Wait for either both conditions or cancellation
yield await(fn () => ($this->conditionA && $this->conditionB) || $this->canceled);
return $this->canceled ? 'canceled' : 'completed';
}
}