Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Created July 3, 2020 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colelawrence/7428e51de4e0b34f9393454740f88ef5 to your computer and use it in GitHub Desktop.
Save colelawrence/7428e51de4e0b34f9393454740f88ef5 to your computer and use it in GitHub Desktop.
/** Lazy promise fetch will reuse promise if one was already created */
export function lazy<F extends () => Promise<any>>(f: F): F {
let once: ReturnType<F> | undefined
// @ts-ignore
return function () {
// @ts-ignore
if (once === undefined) once = f.apply(this, arguments)
return once
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment