Function getOrGenerate

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')