Created
July 3, 2020 13:16
-
-
Save colelawrence/7428e51de4e0b34f9393454740f88ef5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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