Model async results
Derive the resolved value type returned by an async API, including APIs with nested promises.
TypeScript · Type Alias
Awaited<Type> models the result of await and Promise.then by recursively unwrapping promises. It also distributes over unions, preserving non-promise members.
Awaited<Type> models the value produced by await and Promise.then. It recursively removes promise layers, distributes across unions, and preserves non-promise members, making asynchronous API result types match their resolved values.
type Awaited<Type> = ...| Name | Type | Description |
|---|---|---|
Type | type | The type to transform by recursively modeling await or Promise.then unwrapping. |
The recursively unwrapped type of Type; union members are handled individually.
type User = { name: string };
type UserResult = Awaited<Promise<Promise<User>>>;
async function loadUser(): Promise<Promise<User>> {
return Promise.resolve({ name: "Ada" });
}
async function main(): Promise<void> {
const user: UserResult = await loadUser();
console.log(user.name);
}
void main();The nested promise is recursively unwrapped, so UserResult is User and the awaited value can be used as a User.
Derive the resolved value type returned by an async API, including APIs with nested promises.
Describe the resolved element types of promise-oriented collection APIs.
Keep synchronous and asynchronous alternatives represented in one union type.
Promiseasyncawait