Problem
A product uses feature flags to show different behavior to different users. An existing backend returns the state of all flags for the current user. Implement the central utility getFeatureState(featureName) that the rest of the application will call.
The backend is represented by fetchAllFeatures(). It returns a promise for an object whose own keys are feature names and whose values are booleans. The sample below stands in for a network request.
function fetchAllFeatures() {
return new Promise((resolve) => {
setTimeout(() => resolve({
"extended-summary": true,
"feedback-dialog": false,
}), 100);
});
}
async function getFeatureState(featureName) {
// Implement this function.
}
function resetFeatureCache() {
// Clear data when the current user changes or flags must be refreshed.
}
The application uses getFeatureState("extended-summary") to choose between an extended and brief summary. It uses getFeatureState("feedback-dialog") to decide whether to show a feedback button.
Requirements
- Return a promise that resolves to the requested boolean. In particular,
falsemust remainfalse. - Reject with an
Errorfor an empty name, an unknown feature, or a non-boolean feature value. Never return a truthy error string. - Fetch the full map once and reuse it for later lookups. Calls made while a fetch is pending must share the same request.
- Retry a failed fetch up to five total attempts. Wait 100 ms before the first retry and double the delay after each failure. Preserve the final error if all attempts fail. A later call must be able to try again.
resetFeatureCache()must clear the cached data when the current user changes. A request started before reset must not populate the new user's cache.
Check whether a name exists after loading the map. An unknown name is not a network failure and must not trigger retries.