task_all
task_all
Function
Waits for a set of background tasks to complete and returns the success result of each task in the order entered. task_all(tasks) is a synchronous combination waiting function and will not cancel unfinished tasks.
Syntax
values = task_all(tasks)
Parameters
| Parameters | Type | Required | Default value | Description |
|---|---|---|---|---|
| tasks | Array | Yes | None | Array of Tasks to wait for. |
Return value
| Type | Description |
|---|---|
| Array | When all tasks are successful, the result array is returned in the input order; an empty array returns []. |
If any task fails throw or fails normally, task_all() waits for all tasks in the input to complete, then selects the first non-successful result in the order of input: throw will rethrow, and a normal failure returns a runtime error.
Example
t1 = task(fn() { return 1 }) t2 = task(fn() { return 2 }) values = task_all([t1, t2]) // Output: [1,2] print json(values)
slow = task(fn() { sleep(100) return 'slow' }) fast = task(fn() { return 'fast' }) values = task_all([slow, fast]) // Output: ["slow","fast"] print json(values)
Notes
- The parameter must be an array, and all array elements must be Task.
-
task_all([])returns the empty array[]. - Tasks are already executed concurrently when they are created.
task_all()Waiting for results in the order of input will not turn task execution into serial. -
task_all()blocks the current execution flow; should be used with caution in web request hot paths. - Still running tasks will not be forced to be canceled when other tasks fail.