Documents injecute 1.x · Migrating from 0.x
Skip to content

Function: defer() ​

defer<A, R>(factory): (...dependencies) => Promise<Awaited<R>>

Defined in: src/utils/defer.ts:22

Wraps a factory so it accepts promises for any of its arguments: they are awaited before the factory runs, and the wrapped factory returns a promise. Use it to register services that depend on async services in a synchronous container.

Type Parameters ​

A ​

A extends unknown[]

R ​

R

Parameters ​

factory ​

((...args) => R) | (new (...args) => R)

Returns ​

(...dependencies) => Promise<Awaited<R>>

Example ​

ts
app
  .addSingleton('config', () => fetchConfig()) // Promise<Config>
  .addSingleton('db', defer((config: Config) => connect(config.dbUrl)), ['config']);

const db = await app.get('db');

Released under the MIT License.