Async and await
Goal: Type async workflows with Promise<T>.
async functions return Promise<T> when annotated. Await only inside async bodies.
Example
async function fetchText(url: string): Promise<string> {
const res: dynamic = await fetch(url)
const text: dynamic = await res.text()
return text as string
}
async function main(): Promise<void> {
const body: string = await fetchText("https://example.com")
console.log(body.length)
}Key takeaways
- Untyped fetch results start as
dynamic. - Narrow or validate before treating as
string. - Lint SJS-L015 warns on missing
awaitin async paths.
Next: JSX