JavaScript: concurrency without parallelism

Concurrency means several tasks make progress during the same period. Parallelism means they run at the same instant on different cores. JavaScript's async functions give concurrency on one thread: each await suspends a function and lets others run.

JavaScript
const log = [];

async function worker(name, steps) {
  for (let i = 1; i <= steps; i++) {
    log.push(name + i);
    await null;
  }
  return name;
}

Promise.all([worker("A", 3), worker("B", 2), worker("C", 3)]).then((names) => {
  console.log(log.join(" "));
  console.log(names.join(","));
});
Read the full lesson