RunAsync
Executes the given code asynchronously and returns to you a BoxFuture object that you can use to interact with the asynchronously executed code.
A BoxFuture is a subclass of CompletableFuture.
Method Signature
RunAsync(callback=[function], executor=[any])
Arguments
Argument
Type
Required
Description
Default
callback
function
true
The code to execute asynchronously, this can be a closure or lambda.
executor
any
false
The executor to use for the asynchronous execution. This can be an instance of an Executor class, or the name of a registered executor in the AsyncService.
Examples
Run a function asynchronously and get the result
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()
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()
future = runAsync( () => {
return 5;
} ).then( ( Any input ) => {
return input + 2;
} ).error( () => {
return "Error occurred.";
} );
writeOutput( future.get() );
Result: 7
Additional Examples
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
Last updated
Was this helpful?