# 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](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/asyncall)
* [AsyncAllApply](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/asyncallapply)
* [AsyncAny](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/asyncany)
* [AsyncRun](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/asyncrun)
* [ExecutorDelete](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/executordelete)
* [ExecutorGet](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/executorget)
* [ExecutorHas](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/executorhas)
* [ExecutorList](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/executorlist)
* [ExecutorNew](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/executornew)
* [ExecutorShutdown](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/executorshutdown)
* [ExecutorStatus](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/executorstatus)
* [FutureNew](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/futurenew)
* [IsInThread](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/isinthread)
* [isThreadAlive](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/isthreadalive)
* [IsThreadInterrupted](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/isthreadinterrupted)
* [ThreadInterrupt](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/threadinterrupt)
* [ThreadJoin](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/threadjoin)
* [ThreadNew](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/threadnew)
* [ThreadTerminate](https://boxlang.ortusbooks.com/boxlang-language/reference/built-in-functions/async/threadterminate)
