OpenViSTAI · Agents · Blockchain · Crypto
Menu

OPENVIST LEARN · FREE LESSONS

Promises, async and await

Follow asynchronous work without assuming that starting it means it has finished.

Lesson 17 of 25 · About 25 minutes · Beginner · English

By the end of this lesson

Await a promise and catch a rejected asynchronous operation.

Before you start

  • Node.js 24 with a terminal and plain-text editor; the browser-only lesson is labelled separately.
  • Complete lesson 16 or be comfortable with its examples.

Three words to know

Promise
An object representing a future completion or failure.
async function
A function that always returns a promise.
await
Pause the current async flow until a promise settles.

1. An async result is still a promise

Even returning a plain value from an async function produces a fulfilled promise. await unwraps its result for the current flow. Our .mjs source allows top-level await. Other work is not magically made synchronous by awaiting one operation.

Read an async resultJAVASCRIPT · Node.js · save as lesson.mjs
async function describeCount(count) {
  return `records: ${count}`;
}
const pending = describeCount(3);
console.log(pending instanceof Promise);
console.log(await pending);
What you should see · example output
true
records: 3

2. Catch the rejection where you await it

A rejection behaves like a thrown error when awaited. Forgetting await may let the surrounding try finish before the error occurs. Always decide who observes a promise’s failure; do not rely on an unhandled rejection as an application error strategy.

An observed rejectionJAVASCRIPT · Node.js · save as lesson.mjs
async function loadPracticeData() {
  throw new Error("Practice data unavailable");
}
try {
  await loadPracticeData();
} catch (error) {
  console.log(error.message);
}
What you should see · example output
Practice data unavailable

3. Concurrency needs its own contract

Promise.all can combine independent tasks, but it is not a request-rate limiter and does not cancel every other operation when one rejects. The course examples make no network requests. A later HTTP tool would need explicit timeouts, concurrency limits, cancellation and response validation, not just an async keyword.

YOUR TURN

A small check before you continue

Does an async function returning 4 return the number 4 directly? What obtains the fulfilled value?

Show a suggested answer

It returns a promise fulfilled with 4. await gets the value within an allowed async context, or a then callback observes it.

Sources and scope

Documentation and examples reviewed 2026-10-18. Provider screens, package versions and output wording can differ. Examples show expected outcomes; they are not a claim that OpenViST connected to your server or verified your setup.