Terminology (3)Terms used in this lesson
Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.
- Promise
- A JavaScript object representing an eventual fulfilled value or rejection. It may already be settled; the object alone does not specify whether associated work has started.
- await
- Obtains an asynchronous value. JavaScript resumes the continuation asynchronously; a pending Rust .await yields its task, while a ready one can continue in the same poll. C++ future::get instead blocks its thread when needed.
- Executor
- Code that drives tasks, such as a Rust runtime polling futures. Node coordinates JavaScript continuations through jobs and its host event loop; this is not the Rust Future polling interface.
A handle to an eventual result
A promise or future represents the eventual result of an operation: a value on success, or a failure to handle. The handle is a different value from the result it will provide. It can exist while work is pending, or after the result is already available.
Three questions describe the operation’s contract: what initiates the work, how the caller obtains its result, and who remains responsible until it completes. Awaiting is one way to express that dependency. Its effect on the task or thread depends on the native facility; the shared vocabulary does not imply one launch or suspension mechanism.
01A result not ready yet
Promises and futures stand for work in progress
Checkout’s formatter has a promise or future where it needs a price. After that is fixed, price and stock lookups take turns waiting, and declined orders disappear from the audit log. The port needs a clearer contract for each asynchronous result: when its body starts, how its value is obtained, and which work must finish before checkout returns.
Calling a JavaScript async function returns a promise; calling a Rust async fn returns a future for its body. This C++ example uses std::async to obtain std::future, rather than a C++ coroutine task. The caller must obtain the eventual value before using it as a price. JavaScript await resumes through an asynchronous continuation. Rust .await polls its future and yields when pending; a ready result can continue within the current poll. C++ get waits on the calling thread if the shared state is not ready.
async function fetchPrice(): Promise<number> {
console.log('running');
await new Promise((resolve) => setTimeout(resolve, 50));
return 42;
}
async function main(): Promise<void> {
const price = fetchPrice();
console.log('created');
console.log(`awaited ${await price}`);
}
void main();
npx tsc --noEmit --strict main.ts && node main.ts
02Awaiting together
Overlap needs both operations started
The corrected checkout still waits for price before it even starts stock. These two lookups are independent. Calling both JavaScript async functions starts both timer waits; their promises can then be collected. Two Rust async-fn calls only build bodies to be polled, so the supplied join drives both without waiting for one to complete first. C++ can launch the independent calls with std::launch::async before its first get. The default policy permits deferred execution and does not guarantee overlap.
| Language | Overlaps | Still sequential |
|---|---|---|
| TypeScript | Create both promises, then await; or Promise.all | await f(); await g(); |
| Rust | join(f(), g()).await (a runtime or the futures crate supplies join) | let a = f(); let b = g(); a.await; b.await; |
| C++ | Two std::async(std::launch::async, …) before the first get() | std::launch::deferred, or get() before starting the second |
03Results nobody awaits
What happens to work that is never awaited
04Practice
Check out with lookups and audits
The lab uses native timers or sleeps to model lookups and audits, without contacting a payment system, database or network service. Audit lines record completion of the supplied timer. Main prints results when checkout returns. Timing fixtures are authored feasible targets; host scheduling can add delay. The elapsed report does not measure a production checkout or prove CPU parallelism.
The checkout service
A checkout that looks up price and stock with real waits. Use the awaited result, run both lookups at once, then overlap an independent audit while ensuring every order is recorded before its result is reported.
Check this idea later
In the frontend An async server function can still serialize independent I/O. Optional
A product page reads price and stock from two independent services. The app’s catalog module exports readPrice(): Promise<number> and readStock(): Promise<boolean>; neither needs the other result. Review when the second call begins in these server examples.
import { readPrice, readStock } from '@/lib/server/catalog';
export default async function ProductPage() {
const price = await readPrice();
const inStock = await readStock();
return (
<p>
{price}: {inStock ? 'Available' : 'Unavailable'}
</p>
);
}
The Server Component suspends while readPrice is pending. That lets the server do other work, but this invocation has not called readStock yet.
Compare the call boundary with the checkout lookups. Overlap is valid here because the inputs are independent; caches, service limits, streaming and browser paint are outside this diagnostic.
Sources: Next.js: fetching data in Server Components,SvelteKit: loading data and parallel loading.
Sign in to keep your read marks on every device. Sign in
Was this helpful?