Function getOrGenerateSync

Returns a function that fetches a value from a map, or generates and sets it if not present. Undefined is never returned, because if fn yields that, an error is thrown.

See getOrGenerateSync for a synchronous version.

const m = getOrGenerate(new Map(), (key) => {
return key.toUppercase();
});

// Not contained in map, so it will run the uppercase function,
// setting the value to the key 'hello'.
const v = await m(`hello`); // Yields 'HELLO'
const v1 = await m(`hello`); // Value exists, so it is returned ('HELLO')
  • Type Parameters

    • K
    • V
    • Z

    Parameters

    • map: IDictionary<K, V>
    • fn: ((key: K, args?: Z) => V)
        • (key, args?): V
        • Parameters

          • key: K
          • Optionalargs: Z

          Returns V

    Returns ((key: K, args?: Z) => V)

      • (key, args?): V
      • Parameters

        • key: K
        • Optionalargs: Z

        Returns V