Page Contents

Home > @loopback/context > resolveMap

resolveMap() function

Resolve entries of an object into a new object with the same keys. If one or more entries of the source object are resolved to a promise by the resolver function, this method returns a promise which will be resolved to the new object with fully resolved entries.

Signature:

export declare function resolveMap<T, V>(map: MapObject<T>, resolver: (val: T, key: string, values: MapObject<T>) => ValueOrPromise<V>): ValueOrPromise<MapObject<V>>;

Parameters

Parameter Type Description
map MapObject<T> The original object containing the source entries
resolver (val: T, key: string, values: MapObject<T>) => ValueOrPromise<V> A function resolves an entry to a value or promise. It will be invoked with the property value, the property name, and the source object.

Returns:

ValueOrPromise<MapObject<V>>

Example

  • Example 1: resolve all entries synchronously
const result = resolveMap({a: 'x', b: 'y'}, v => v.toUpperCase());

The result will be {a: 'X', b: 'Y'}.

  • Example 2: resolve one or more entries asynchronously
const result = resolveMap({a: 'x', b: 'y'}, v =>
  Promise.resolve(v.toUpperCase()),
);

The result will be a promise of {a: 'X', b: 'Y'}.