Page Contents

Home > @loopback/context > Context > getSync

Context.getSync() method

Get the synchronous value bound to the given key, optionally return a (deep) property of the bound value.

This method throws an error if the bound value requires async computation (returns a promise). You should never rely on sync bindings in production code.

Signature:

getSync<ValueType>(keyWithPath: BindingAddress<ValueType>, options?: ResolutionOptions): ValueType | undefined;

Parameters

Parameter Type Description
keyWithPath BindingAddress<ValueType> The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve.
options ResolutionOptions (Optional) Options for resolution.

Returns:

ValueType | undefined

The bound value, or undefined when an optional binding is not found.

Example

// get "rest" property from the value bound to "config"
// use "undefined" when no config is provided
const config = await ctx.getSync<RestComponentConfig>('config#rest', {
  optional: true
});