# RunAsync

Executes the given code asynchronously and returns to you a BoxFuture object which inherits from CompletableFuture.

This way you can create fluent asynchronous code that can be chained and composed.

## Method Signature

```
RunAsync(callback=[function], executor=[any])
```

### Arguments

| Argument   | Type       | Required | Description                                                                                                                                                              | Default |
| ---------- | ---------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------- |
| `callback` | `function` | `true`   | <p>The code to execute asynchronously, this can be a closure<br>or lambda.</p>                                                                                           |         |
| `executor` | `any`      | `false`  | <p>The executor to use for the asynchronous execution. This<br>can be an instance of an Executor class, or the name of a<br>registered executor in the AsyncService.</p> |         |

## Examples

### Run a function asynchronously and get the result

[Run Example](https://try.boxlang.io/?code=eJxLKy0pLUpVsFUoKs1zLK7MS9ZQ0NBUsLVTqObiLEoFyuUpKHmk5uTkK4TnF%2BWkKCpZc9UqaFpzlRdllqT6l5YUlJZoKKSBDdFLTy0B6gVKAgCTuhrL)

```java
future = runAsync( () => {
	return "Hello World!";
} );
writeOutput( future.get() );

```

Result: Hello World!

### Run a function after the asynchronous function and use a five milliseconds timeout when calling get()

[Run Example](https://try.boxlang.io/?code=eJxdjUEKwkAMRdfOKf5yBqEFxVWp0BN4iJJqwEbJJEgR795QXbkK%2FPf%2Fy%2BTmSuihLkNdZMzIBf0Z77RTCiY4demD0tiNJCAGWcDydMNf7xvucdj6XVKqfrdQT9uP5kqWcQyCto3LFcYzPdwyC%2BZa0kvZ6OIWmozfOjwr13wyHQ%3D%3D)

```java
future = runAsync( () => {
	return 5;
} ).then( ( Any input ) => {
	return input + 2;
} );
result = future.get( 3 ); // 3 is timeout(in ms)
writeOutput( result );

```

Result: 5

### Run a function asynchronously with then() and error()

```java
future = runAsync( () => {
	return 5;
} ).then( ( Any input ) => {
	return input + 2;
} ).error( () => {
	return "Error occurred.";
} );
writeOutput( future.get() );

```

Result: 7

### Additional Examples

[Run Example](https://try.boxlang.io/?code=eJxdjU0OgkAMhdfOKd5yiAmgLgkmnMBDaNFJpJCZNoQY727HceWufT%2FfG1U0EnpE5SFtfPXwFfozXm4XyTzGoe3cG1UtD2JzMfCGwIsK%2FoJF3ONYCp276bR4jGXC%2FkhJn2JjRarvJN7wZqFp8hESJEw0q%2FjAmFLl1hiELipG9vj1jZTz81fNnVPrPrwgPNQ%3D)

```java
future = runAsync( () => {
	return 10;
} ).then( ( Any input ) => {
	return input + 20;
} );
dump( future );
result = future.get( 10 ); // 10 is timeout(in ms)
writeOutput( result );
 // output is 30

```

## Related

* [AsyncAll](/boxlang-language/reference/built-in-functions/async/asyncall.md)
* [AsyncAllApply](/boxlang-language/reference/built-in-functions/async/asyncallapply.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)
* [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: 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:

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

The question should be specific, self-contained, and written in natural language.
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.
