Pause workflow execution until a condition becomes true
The await pattern allows workflows to pause execution until a specific condition evaluates to true. This is essential for workflows that need to wait for external events, signals, or state changes.
The await() function pauses workflow execution and periodically checks a condition. When the condition returns true, the workflow resumes. This pattern is deterministic and replay-safe.
For scenarios where you need a maximum wait time, use awaitWithTimeout():
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 48 hours for approval $result = yield awaitWithTimeout( '48 hours', fn () => $this->approved ); if ($result) { yield activity(ProcessApprovalActivity::class); return 'approved'; } // Timeout occurred yield activity(HandleTimeoutActivity::class); return 'timeout'; }}
You can also use integer seconds or CarbonInterval:
use Carbon\CarbonInterval;// Wait up to 2 hours (7200 seconds)yield awaitWithTimeout(7200, fn () => $this->ready);// Wait up to 1 weekyield awaitWithTimeout(CarbonInterval::weeks(1), fn () => $this->completed);
If the timeout occurs before the condition becomes true, awaitWithTimeout() returns false.
You can use await without any activities for purely signal-driven workflows:
class SignalDrivenWorkflow extends Workflow{ private bool $ready = false; #[SignalMethod] public function markReady(): void { $this->ready = true; } public function execute() { // Entire workflow is just waiting for a signal yield await(fn () => $this->ready); return 'completed'; }}