12 of 15
Async code
spawn runs a function as a separate task and returns a promise; await waits for its result. Functions marked async can await inside them:
A channel passes values between tasks. for (value in channel) waits for each value and stops when the channel is closed:
pleno
import { Channel } from "std:concurrency";
let numbers = Channel(10);
spawn(fn() {
numbers.send(1);
numbers.send(2);
numbers.close();
});
for (n in numbers) {
print(n);
}RunYour turn
Start three tasks that each return a word, then wait for all of them with Promise.all and print the joined sentence.