Exercise 20: Fetch with Response Transformation

Problem Statement

Create a function `fetchWithTransform(url, transform)` that transforms response. Example: fetchWithTransform(url, (data) => data.items)

Sample Output:

await fetchWithTransform(url, (data) => data.items)
// Transforms response data

Solution

const fetchWithTransform = async (url, transform) => {
  const res = await fetch(url);
  const data = await res.json();
  return transform(data);
};

Explanation

Overall Goal:

  • Response ko transform function se modify karna.

Real world:

  • Data transformation: response mapping.
  • API adapters: data transformation.