Created
November 24, 2023 20:51
-
-
Save lmiller1990/26eb3c2d76eb8c122a0012c1e47dc15e to your computer and use it in GitHub Desktop.
Improved Solution
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
import pdefer from "https://unpkg.com/[email protected]/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