TypeScript/Async/Lesson 04
async / await — Annotating the Return Type Lets Callers Infer Automatically
30 min·theory
This chapter
4/7
TypeScript
async / await — Annotating the Return Type Lets Callers Infer Automatically
💡 Why Should You Learn This? — An async Function's Signature Is a Contract
🎯
With JS async functions, you have to read through the entire function body to know what they return.
💼
With TS async functions, if you pin `Promise` in the signature, callers can use them safely without reading the implementation.
⚡
The result type of `await` is inferred automatically — a type you pin once flows through to the end of the await chain.
🔗
The `err` in try/catch has been `unknown` since TS 4.4+. You must narrow it with something like `instanceof Error` before accessing `.message`.
🏢 실무에서는
Next.js Server Components, Server Actions, and API Route handlers are all async functions. When you explicitly annotate the return type, every client and server location that imports and uses that function gets parameter and return-value hints from the IDE. Without annotation, forced casts like `as User` accumulate at every call site.
async Function = Always Returns a Promise, await = Unwrapping It
1. An async function's return type is automatically wrapped in Promise
- ▸A function marked
asyncalways returns a Promise. - ▸The return type must be in the form
Promise<...>(specifying any other type in the signature causes a compile error). - ▸Returning
return valuein the body automatically wraps it asPromise.resolve(value). - ▸Throwing
throw errin the body automatically becomesPromise.reject(err).
2. The result of await is inferred automatically
3. The err in catch is unknown (TS 4.4+)
4. A Promise-returning function and an async function are equivalent
Choose whichever you prefer, but if you want to use await inside the body, async is required.
💡 💡 async/await TypeScript Essentials — Top 5
1. Explicitly annotate the return type of async functions in the signature
The signature alone tells callers what to expect.
2. You can leave the await result to inference
Annotating improves readability, but omitting is safe too.
3. err is unknown — narrow it before use
useUnknownInCatchVariables: true in tsconfig is the default for TS 4.4+ strict mode.
4. Don't double-wrap a Promise inside an async function
5. Destructure the result of Promise.all as a tuple (already covered in the promise lesson)
⚡ Try It Yourself — async/await
A runnable version with types stripped. Verify sequential processing with await and error handling with try/catch.
✏️ JS 코드
📟 Console output
▶ Press the Run button
⚠️ Runs in a browser sandbox — only console.log() is supported; alert/fetch are not.
Check Your Understanding
In TS 4.4+, why does `try { ... } catch (err) { console.log(err.message); }` produce a compile error?
Read this first: Promise<T> — JS vs TS Differences