> For the complete documentation index, see [llms.txt](https://boxlang.ortusbooks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/asyncallapply.md).

# AsyncAllApply

This function can accept an array of items or a struct of items and apply a function to each of the item's in parallel.

The `mapper` argument receives the appropriate item and must return a result.

The \`errorHandler\` is optional and will be called if the mapper function throws an exception. The error handler receives the exception and the item that caused it, allowing you to handle errors gracefully.

The result is a future that will return an array of results, or a struct of results if the input was a struct.

The \`executor\` argument is optional and allows you to specify a custom executor for the asynchronous operations. If not provided, the common fork-join pool will be used.

The \`timeout\` and \`timeUnit\` arguments allow you to specify a timeout for the operation. If the operation does not complete within the specified timeout, it will throw a TimeoutException. The allowed time units are: \`DAYS\`, \`HOURS\`, \`MINUTES\`, \`SECONDS\`, \`MILLISECONDS\`, \`MICROSECONDS\`, \`NANOSECONDS\`.

Example usage:

```
 // Array
 allApply( items, ( item ) => item.getMemento() )
 // Struct: The result object is a struct of `key` and `value`
 allApply( data, ( item ) => item.key & item.value.toString() )
 
```

## Method Signature

```
AsyncAllApply(items=[any], mapper=[function], errorHandler=[function], executor=[any], timeout=[long], timeUnit=[any])
```

### Arguments

| Argument       | Type       | Required | Description                                                                                                                                                        | Default   |
| -------------- | ---------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------- |
| `items`        | `any`      | `true`   | The items to apply the function to. Can be an array or a struct.                                                                                                   |           |
| `mapper`       | `function` | `true`   | The function to apply to each item. It receives the item as an argument and must return a result.                                                                  |           |
| `errorHandler` | `function` | `false`  | Optional function to handle errors. It receives the exception and the item that caused it.                                                                         |           |
| `executor`     | `any`      | `false`  | Optional executor to use for the asynchronous operations. If not provided, the common fork-join pool will be used.                                                 |           |
| `timeout`      | `long`     | `false`  | Optional timeout for the operation. If the operation does not complete within this time, it will throw a TimeoutException.                                         | `0`       |
| `timeUnit`     | `any`      | `false`  | Optional time unit for the timeout. Defaults to seconds. Allowed values are: `DAYS`, `HOURS`, `MINUTES`, `SECONDS`, `MILLISECONDS`, `MICROSECONDS`, `NANOSECONDS`. | `SECONDS` |

## Examples

### Apply a function to each item in parallel

Accepts an array of items and a function, applying the function to each item in parallel.

```java
items = [ 1, 2, 3, 4, 5 ];
results = allApply( items, ( n ) => n * n ).get();
writeOutput( results.toString() );

```

Result: \[1, 4, 9, 16, 25]

### Process a struct of items

```java
data = { a: 10, b: 20, c: 30 };
results = allApply( data, ( v ) => v * 2 ).get();
writeOutput( results.len() );

```

Result: 3

### Using a named executor

```java
executorNew( "myPool", "fixed", 4 );
results = allApply( [ 1, 2, 3 ], ( n ) => n + 1, "myPool" ).get();
writeOutput( results.toString() );

```

Result: \[2, 3, 4]

## Related

* [AsyncAll](/boxlang-language/reference/built-in-functions/async/asyncall.md)
* [AsyncAny](/boxlang-language/reference/built-in-functions/async/asyncany.md)
* [AsyncRun](/boxlang-language/reference/built-in-functions/async/asyncrun.md)
* [ExecutorDelete](/boxlang-language/reference/built-in-functions/async/executordelete.md)
* [ExecutorGet](/boxlang-language/reference/built-in-functions/async/executorget.md)
* [ExecutorHas](/boxlang-language/reference/built-in-functions/async/executorhas.md)
* [ExecutorList](/boxlang-language/reference/built-in-functions/async/executorlist.md)
* [ExecutorNew](/boxlang-language/reference/built-in-functions/async/executornew.md)
* [ExecutorShutdown](/boxlang-language/reference/built-in-functions/async/executorshutdown.md)
* [ExecutorStatus](/boxlang-language/reference/built-in-functions/async/executorstatus.md)
* [FutureNew](/boxlang-language/reference/built-in-functions/async/futurenew.md)
* [IsInThread](/boxlang-language/reference/built-in-functions/async/isinthread.md)
* [isThreadAlive](/boxlang-language/reference/built-in-functions/async/isthreadalive.md)
* [IsThreadInterrupted](/boxlang-language/reference/built-in-functions/async/isthreadinterrupted.md)
* [RunAsync](/boxlang-language/reference/built-in-functions/async/runasync.md)
* [ThreadCurrent](/boxlang-language/reference/built-in-functions/async/threadcurrent.md)
* [ThreadInterrupt](/boxlang-language/reference/built-in-functions/async/threadinterrupt.md)
* [ThreadJoin](/boxlang-language/reference/built-in-functions/async/threadjoin.md)
* [ThreadNew](/boxlang-language/reference/built-in-functions/async/threadnew.md)
* [ThreadTerminate](/boxlang-language/reference/built-in-functions/async/threadterminate.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/asyncallapply.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
