Synchronous callback implementation
An Auth.js callback can compute its result without I/O.
Auth.js · Type Alias
Awaitable<T> is Auth.js's public type for a result that can be supplied immediately as T or asynchronously through PromiseLike<T>. It lets callbacks and provider hooks support either synchronous or asynchronous implementations.
Awaitable<T> is a union that permits either a value of type T or a PromiseLike<T>. Auth.js uses it for extension points such as callbacks, allowing implementations to complete immediately or return a thenable that resolves to the declared result.
type Awaitable<T> = T | PromiseLike<T>;type Awaitable<T> = T | PromiseLike<T>;
const immediate: Awaitable<string> = "done";
const deferred: Awaitable<string> = Promise.resolve("done");
async function readValue(value: Awaitable<string>): Promise<string> {
return await value;
}
console.log(await readValue(immediate));
console.log(await readValue(deferred));The same function accepts either an immediate string or a promise-like string, then normalizes both with await.
An Auth.js callback can compute its result without I/O.
A callback or provider hook needs database, network, or other asynchronous work.
A library function should accept either a direct result or a thenable.
Awaitable<T> means T or PromiseLike<T>. It lets an Auth.js callback return its result directly or asynchronously while preserving the result type.
Follow-up: How would a consumer handle both forms? Use await: JavaScript assimilates thenables, so awaiting the value produces T.
A callback typed as Awaitable<string> may return a string immediately or a promise-like object resolving to a string. This supports both synchronous and asynchronous implementations without separate callback types.
Follow-up: Why is this useful in library APIs? Callers can provide lightweight synchronous handlers while still supporting I/O-bound handlers.
The declaration is type Awaitable<T> = T | PromiseLike<T>. The generic parameter T is the eventual logical result, while the union permits either immediate or deferred delivery.
Follow-up: How does it differ from Promise<T>? Promise<T> requires a promise object; Awaitable<T> also accepts T directly and any compatible thenable.
AwaitedPromiseLikeAuth.js callbacksProfileCallback