aiChatAsync
Asynchronous version of aiChat() that returns a BoxLang Future for non-blocking AI requests.
Syntax
aiChatAsync(messages, params, options)Parameters
messages
any
Yes
The messages to pass to the AI model (string, struct, array, or AiMessage)
params
struct
No
Request parameters for the AI provider
options
struct
No
Request options (provider, apiKey, returnFormat, timeout, logging)
Parameters are identical to aiChat().
Returns
Returns a BoxLang Future object that will eventually contain the AI response. Use .get() to retrieve the result (blocking) or .then() for callbacks.
Examples
Basic Async Request
// Start async request
future = aiChatAsync( "What is BoxLang?" );
// Do other work...
doSomethingElse();
// Get result (blocks until ready)
response = future.get();
println( response );Multiple Concurrent Requests
With Callback
Error Handling
With Timeout
Parallel Processing Pipeline
Background Processing
Future Methods
.get(timeout)
.get(timeout)Blocks until result available, returns the AI response.
.then(callback)
.then(callback)Executes callback when result ready, returns new Future.
.onError(callback)
.onError(callback)Handles errors in the Future chain.
.isDone()
.isDone()Check if computation complete without blocking.
.cancel()
.cancel()Attempt to cancel the computation.
Use Cases
✅ Multiple Independent Requests
✅ Long-Running Operations
✅ Background Tasks
❌ Simple Single Request
Notes
Extends aiChat(): All
aiChat()parameters and options supportedNon-blocking: Returns immediately, computation runs in background
Thread pool: Uses BoxLang's async executor
Error propagation: Errors in async code bubble up to
.get()or.onError()Same return formats: Supports all
returnFormatoptionsChaining: Can chain multiple
.then()calls for pipeline processing
Related Functions
aiChat()- Synchronous versionaiChatStream()- Streaming version with callbacksaiAgent()- For complex autonomous tasks
Performance Tips
Last updated