Function updateOutdated

Calls the async fn to generate a value if there is no prior value or interval has elapsed since value was last generated.

const f = updateOutdated(async () => {
const r = await fetch(`blah`);
return await r.json();
}, 60*1000);

// Result will be JSON from fetch. If fetch happened already in the
// last 60s, return cached result. Otherwise it will fetch data
const result = await f();

Callback fn is passed how many milliseconds have elapsed since last update. Its minimum value will be interval.

const f = updateOutdated(async elapsedMs => {
// Do something with elapsedMs?
}, 60*1000;

There are different policies for what to happen if fn fails. slow is the default.

  • fast: Invocation will happen immediately on next attempt
  • slow: Next invocation will wait interval as if it was successful
  • backoff: Attempts will get slower and slower until next success. Interval is multipled by 1.2 each time.
  • Type Parameters

    • V

      Return type of fn

    Parameters

    • fn: ((elapsedMs?: number) => Promise<V>)

      Async function to call. Must return a value.

        • (elapsedMs?): Promise<V>
        • Parameters

          • OptionalelapsedMs: number

          Returns Promise<V>

    • interval: Interval

      Maximum age of cached result

    • updateFail: UpdateFailPolicy = ...

      slow by default

    Returns (() => Promise<V>)

    Value

      • (): Promise<V>
      • Returns Promise<V>