@loopback/docs
Version:
Documentation for LoopBack 4
48 lines (30 loc) • 1.09 kB
Markdown
lang: en
title: 'API docs: context.binding.todynamicvalue'
keywords: LoopBack 4.0, LoopBack 4
sidebar: lb4_sidebar
permalink: /doc/en/lb4/apidocs.context.binding.todynamicvalue.html
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) > [@loopback/context](./context.md) > [Binding](./context.binding.md) > [toDynamicValue](./context.binding.todynamicvalue.md)
## Binding.toDynamicValue() method
Bind the key to a computed (dynamic) value.
<b>Signature:</b>
```typescript
toDynamicValue(factoryFn: () => ValueOrPromise<T>): this;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| factoryFn | <code>() => ValueOrPromise<T></code> | The factory function creating the value. Both sync and async functions are supported. |
<b>Returns:</b>
`this`
## Example
```ts
// synchronous
ctx.bind('now').toDynamicValue(() => Date.now());
// asynchronous
ctx.bind('something').toDynamicValue(
async () => Promise.delay(10).then(doSomething)
);
```