Skip to content

Instantly share code, notes, and snippets.

@lmiller1990
Created November 24, 2023 20:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmiller1990/26eb3c2d76eb8c122a0012c1e47dc15e to your computer and use it in GitHub Desktop.
Save lmiller1990/26eb3c2d76eb8c122a0012c1e47dc15e to your computer and use it in GitHub Desktop.
Improved Solution
import pdefer from "https://unpkg.com/p-defer@4.0.0/index.js";
/** @returns {Promise<Record<string, boolean>} */
async function fetchAllFeatures() {
console.log("Making network call...");
return new Promise((res, rej) => {
setTimeout(() => {
res({});
}, 500);
});
}
let promise
/** @param {string} flag */
async function getFeatureFlag(flag) {
if (!promise) {
promise = new Promise(res => {
res(fetchAllFeatures())
})
}
return promise.then((flags) => {
return flags[flag] ?? false
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment