Running work at the same time

Futures, runAsync and then, and how the browser build runs them.

On this page

A future stands for a value that is not ready yet. runAsync starts some work and hands you a future straight away. You call get() on it when you need the result.

Your first future

answer = runAsync( () => 6 * 7 );

println( "started" );
println( answer.get() );

Output:

started
42

Several at once

Start more than one future, then collect the results.

slow = runAsync( () => {
    sleep( 20 );
    return "slow";
} );
fast = runAsync( () => "fast" );

println( fast.get() & " then " & slow.get() );

Output:

fast then slow

Chaining

then runs a function on the result when it arrives, and gives you a new future.

result = runAsync( () => 10 ).then( n => n * 2 );
println( result.get() );

Output:

20

Measuring time

getTickCount() returns milliseconds, and sleep( ms ) pauses.

start = getTickCount();
sleep( 30 );
elapsed = getTickCount() - start;
println( elapsed >= 30 );

Output:

true

In the browser

BoxLang futures take turns on the VM's thread, in both native and browser builds. Native I/O can run independently; CPU-heavy callbacks do not run across multiple cores. The playground stops runs after five seconds.

Try it

Start three futures that each sleep for a different time and return a different word. Print them in the order you started them.

Edit this page Download Markdown Last updated Oct 27, 2026, 1:45:34 PM Report an issue
Lesson 8 of 10 - Learn MatchBox