Home
← All questions

Implement a JavaScript Feature Flag Utility

MediumPremiumJavaScript

Asked at Atlassian

JavaScript interview practice: Build getFeatureState with cached feature data, one shared in-flight request, bounded retries, and safe handling of missing or disabled…

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

  1. Return a promise that resolves to the requested boolean. In particular, false must remain false.
  2. Reject with an Error for an empty name, an unknown feature, or a non-boolean feature value. Never return a truthy error string.
  3. Fetch the full map once and reuse it for later lookups. Calls made while a fetch is pending must share the same request.
  4. 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.
  5. 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.

Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium