Errors, packages and async · 12 min · free lesson

Promises vs futures

A Promise starts running when it is created; a Rust future does nothing until it is polled, usually by .await inside a runtime such as Tokio.

Core idea

Futures are lazy: calling an async fn only creates a future, and no code inside it runs until something polls it.

JavaScript (Node.js) and Rust, side by side

JavaScript (Node.js) Each call starts the async body immediately, so fetching prints twice even though one promise is never used.
async function fetchPrice() {
  console.log("fetching"); // runs as soon as fetchPrice() is called
  return 42;
}

const pending = fetchPrice();
console.log("created");
pending.then((price) => console.log(`price = ${price}`));

const unused = fetchPrice(); // still runs, although nobody awaits it
Rust created prints before fetching: the future runs only when block_on polls it, and the unused one never runs.
use std::future::Future;
use std::pin::pin;
use std::task::{Context, Poll, Waker};

async fn fetch_price() -> u32 {
    println!("fetching");
    42
}

// A minimal executor for futures that never wait; real programs use a runtime such as Tokio.
fn block_on<F: Future>(future: F) -> F::Output {
    let mut future = pin!(future);
    let mut cx = Context::from_waker(Waker::noop());
    loop {
        if let Poll::Ready(value) = future.as_mut().poll(&mut cx) {
            return value;
        }
    }
}

fn main() {
    let pending = fetch_price();
    println!("created");
    println!("price = {}", block_on(pending));
    let _unused = fetch_price();
}
What changes
JavaScript (Node.js)Rust
fetchPrice() runs the body up to the first await immediately.fetch_price() runs nothing; the body starts on the first poll.
The event loop is built into Node.js.An executor such as Tokio must poll the top-level future.
A promise that nobody awaits still runs to completion.A future that is never polled never runs, and dropping it cancels it.
Promise.all([a, b]) runs promises concurrently.join!(a, b) polls both futures concurrently inside one task.

Understand it

Calling an async function in JavaScript runs its body synchronously up to the first await, and the event loop keeps the rest going. That is why the example prints fetching before created, and why the unused second call still logs. Calling a Rust async fn runs nothing: it returns a value that implements Future, a state machine waiting to be polled. Only polling, normally through .await, executes the body.

Rust has no built-in event loop. Something must poll the top-level future: an executor such as the Tokio runtime behind #[tokio::main], or, in this example, a ten-line block_on that polls in a loop. That block_on only suits futures that never wait on I/O; a real runtime parks the thread and uses the Waker to resume work when a socket or timer is ready. .await itself is only allowed inside async functions and blocks (E0728).

Laziness has consequences you can use. A future that is dropped before it completes simply stops, which is how timeouts and select! cancel work, whereas a JavaScript promise cannot be cancelled from outside; there is only an AbortSignal the callee may honour. Writing let pending = fetch_price(); and awaiting it later starts nothing early. To run several futures concurrently, combine them with join! or spawn them as tasks, the counterparts of Promise.all and of starting promises without awaiting them.

rustc helps with the most common mistake. A future that is created and never used triggers the unused_must_use warning, unused implementer of Future that must be used. Treat that warning as an error in async code; it almost always means a missing .await.

The JavaScript (Node.js) habit, written in Rust

This is how the JavaScript (Node.js) version usually looks on a first attempt in Rust. Predict what rustc says before you read the explanation.

async fn save(record: &str) {
    println!("saved {record}");
}

fn main() {
    save("invoice-7");
    println!("done");
}

Practise this code in Playground ↗

Try it

Predict the output of the example, then move println!("created") after the block_on call and predict again. Finally replace let _unused = fetch_price(); with fetch_price(); and read the warning.

What success looks like: Your predictions match the program: created before fetching, and the unused future never prints.

Check your understanding

What does the practice program print?

Reveal the answer
  1. saved invoice-7 then done, like calling an async function in Node.js
  2. Only done: the future returned by save is never polled
  3. Nothing: rustc rejects calling an async fn without .await

The call creates a future and drops it at the end of the statement. rustc warns that it must be used, but the program builds and prints only done.

Go deeper

Common misconception: “Calling an async fn starts it in the background, like calling an async function in Node.js.”

Calling an async fn only builds a future. Nothing runs until it is polled, normally by .await inside a runtime.

  1. Create the future: let pending = fetch_price(); stores a future. No line inside fetch_price has run yet.
  2. Poll it: block_on(pending) polls once. The body prints fetching and returns 42 without waiting, so the first poll is already Ready.
  3. Compare with evidence: Run it: created, fetching, price = 42. The _unused future is dropped at the end of main without printing anything.

Transfer challenge: Write async fn total() -> u32 that awaits fetch_price() twice and adds the results. Run it with block_on and predict how many times fetching prints, then compare with a JavaScript version that uses Promise.all.

Connections

  • npm and package.json vs Cargo: package.json and npm mapped to Cargo
  • Futures: async fn, await, and laziness
Side-by-side comparison: the JavaScript (Node.js) version on the left, the Rust version on the right, then 4 rows naming what changes.
Promises vs futures: the same task in JavaScript (Node.js) and in Rust, with the differences that matter.
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Promises vs futures (courseId "rust-for-nodejs-developers", lessonId "node-promises-to-futures")
Link: https://crabrix.com/learn/rust-for-nodejs-developers/node-promises-to-futures/
Goal: Futures are lazy: calling an async fn only creates a future, and no code inside it runs until something polls it.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=rust-for-nodejs-developers/node-promises-to-futures) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.