jsrosetta

Async

Async/Await

How Node.js's Promise and async/await compare to goroutines (Go), Future + tokio (Rust), Swift concurrency, and CompletableFuture (Java).

Node.js runs a single-threaded event loop: await yields control back to the event loop until the Promise settles. Each language below solves the same problem with its own model. Read the notes carefully, because the difference lies in the runtime, not just the syntax.

Waiting for a task

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
 
async function fetchUser(id) {
  await sleep(100);
  return { id, name: `user-${id}` };
}
 
const user = await fetchUser(1); // top-level await in an ES module
console.log(user.name);

Running in parallel (Promise.all)

const users = await Promise.all([1, 2, 3].map(fetchUser));
console.log(users.length); // 3, total time ~100ms

Key differences to remember

Node.js Go Rust Swift Java
Async unit Promise goroutine Future Task CompletableFuture
Starts running when Promise is created go is called .awaited/polled async let/Task is called supplyAsync is called
Runtime built-in event loop built-in choose your own (tokio…) built-in built-in thread pool
True multithreading no (except workers) yes yes yes yes

Reference: github.com/miguelmota/golang-for-nodejs-developers#asyncawait