---
title: Running work at the same time
summary: Futures, runAsync and then, and how the browser build runs them.
icon: phosphor-duotone:lightning
tags: [course]
---

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

```boxlang title="future.bxs"
answer = runAsync( () => 6 * 7 );

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

Output:

```text
started
42
```

## Several at once

Start more than one future, then collect the results.

```boxlang title="two-futures.bxs"
slow = runAsync( () => {
    sleep( 20 );
    return "slow";
} );
fast = runAsync( () => "fast" );

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

Output:

```text
fast then slow
```

## Chaining

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

```boxlang title="then.bxs"
result = runAsync( () => 10 ).then( n => n * 2 );
println( result.get() );
```

Output:

```text
20
```

## Measuring time

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

```boxlang title="timing.bxs"
start = getTickCount();
sleep( 30 );
elapsed = getTickCount() - start;
println( elapsed >= 30 );
```

Output:

```text
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.
