Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Create a Reusable Fetch Function

Other ways to make fetch modular and easier to maintain?

He says in the video you can find more info in the teacher's notes, but I don't see anything. Anyone have a link I can use to view better ways to use fetch aside from the fetchData function in the tutorial?

1 Answer

Perhaps this doesn't answer your question, but you may like this pattern:

async function doSomething {
  const data = await (await fetch(url)).json();
  // deal with data
};

The outer call (await (expression).json()) waits for the inner call (await fetch(url)) to resolve before firing, and may save you having to write a fetch function requiring maintenance in the first place. This pattern can be found in this FreeCodeCamp tutorial (rest of the tutorial irrelevant for this particular issue).