Page Contents

Home > @loopback/context > Binding > toDynamicValue

Binding.toDynamicValue() method

Bind the key to a computed (dynamic) value.

Signature:

toDynamicValue(factoryFn: () => ValueOrPromise<T>): this;

Parameters

Parameter Type Description
factoryFn () => ValueOrPromise<T> The factory function creating the value. Both sync and async functions are supported.

Returns:

this

Example

// synchronous
ctx.bind('now').toDynamicValue(() => Date.now());

// asynchronous
ctx.bind('something').toDynamicValue(
 async () => Promise.delay(10).then(doSomething)
);