Creates an asynchronous (non-blocking) job for the given action and arguments.
Signature
function async(
ActionInterface|string|callable $_,
mixed ...$argument
): JobInterface
Parameters
$_
ActionInterface|class-string<ActionInterface>|callable
required
The action to run. Can be:
- An
ActionInterface instance
- A class string of an Action class
- Any PHP callable (closure, function, invokable object)
Named arguments for the action’s run method. Values can be:
- Literal values (strings, integers, arrays, etc.)
variable() references for runtime variables
response() references to previous job outputs
Returns
An asynchronous job that runs concurrently with other async jobs when possible.
Usage
Parallel image processing
use function Chevere\Workflow\{workflow, async, sync, variable, response};
$workflow = workflow(
// These three jobs run in parallel
thumb: async(
ImageResize::class,
file: variable('image'),
width: 150,
height: 150
),
medium: async(
ImageResize::class,
file: variable('image'),
width: 800
),
large: async(
ImageResize::class,
file: variable('image'),
width: 1920
),
// This waits for all resizing to complete
store: sync(
StoreFiles::class,
thumb: response('thumb'),
medium: response('medium'),
large: response('large')
)
);
Parallel API calls
use function Chevere\Workflow\{workflow, async, sync, response};
$workflow = workflow(
// Fetch from multiple endpoints in parallel
users: async(
FetchUrl::class,
url: 'https://api.example.com/users'
),
posts: async(
FetchUrl::class,
url: 'https://api.example.com/posts'
),
comments: async(
FetchUrl::class,
url: 'https://api.example.com/comments'
),
// Combine results after all fetches complete
combine: sync(
CombineData::class,
users: response('users'),
posts: response('posts'),
comments: response('comments')
)
);
Mixed sync and async
$workflow = workflow(
// Setup runs first (sync)
setup: sync(
SetupAction::class,
config: variable('config')
),
// These run in parallel after setup
task1: async(
Task1::class,
data: response('setup')
),
task2: async(
Task2::class,
data: response('setup')
)
);
Notes
- Async jobs run concurrently when they have no dependencies on each other
- Jobs with dependencies still wait for their requirements to complete
- The workflow engine automatically determines optimal execution order
- For blocking execution, use
sync() instead
- Async jobs are ideal for I/O-bound operations like API calls or file processing