TypeScript Async Programming
An async function returns a Promise. Its type describes the eventual resolved value; a rejection is still possible and must be handled by the calling workflow.
How it works
A desk refresh can be represented as an asynchronous count lookup even when the teaching fixture resolves locally. Await makes the dependency clear: the summary cannot be formed until the count arrives. The example also shows a thrown Error turning into a rejected Promise. These are local functions; they do not test a network request, server availability or a retry policy.
Run the code
Try in playground loads this source without running it. Choose Run when ready. The existing TypeScript 5.8.3 worker transpiles and executes one file; it does not perform full semantic type checking, load npm imports or provide DOM/Node APIs. Use the local compiler for the type contract.
For a full check, save the three downloads together in a new folder, run npm install, then npm run check. Use npm run build followed by npm start to execute the checked JavaScript with Node. The supplied project enables strict checking, indexed-access checks and exact optional properties.
Example
async function loadPackets(code: string): Promise<number> {
if (code !== "BE") throw new Error("Unknown lot");
return 9;
}
async function main(): Promise<void> {
console.log(await loadPackets("BE"));
try { await loadPackets("XX"); } catch { console.log("Lookup rejected"); }
}
await main();
Expected result
9 Lookup rejected
The valid lookup resolves to nine. The rejected lookup is awaited inside the try, so its failure reaches the intended catch. A Promise’s generic type is not a declaration of which errors it may throw. For typed expected failures, return an explicit result union.