Run In Background

Run part of a program while the rest of it is doing something else.

control.inBackground(() => {
})

For more information, read The micro:bit - a reactive system. It is pretty advanced!

Example

This program shows how running in the background can say what is stored in a variable like num, while another part (on button pressed) changes what is stored there.

let num = 0
control.inBackground(() => {
    while (true) {
        basic.showNumber(num, 150)
        basic.pause(100)
    }
})
input.onButtonPressed(Button.A, () => {
    num++;
})

This program does the same thing, but in a more usual way, with a forever loop.

let num = 0
basic.forever(() => {
    basic.showNumber(num, 150)
})
input.onButtonPressed(Button.A, () => {
    num++;
})

See also

while, forever, on button pressed