If you remember the days of having to use the XMLHttpRequest (XHR) construct to make HTTP requests in JavaScript, you can especially appreciate the more modern Fetch API. With XHR requests, JavaScript developers are limited to using callbacks in order to model asynchronicity. Fetch allows you to create HTTP requests, which are easily handled using JavaScript Promises. In terms of handling asynchronous control-flow, Promises are a massive step up from callbacks. This is especially true when you need to chain multiple requests, one after the other.

In this guide, you will learn how to effectively handle the chaining of fetch calls in order to avoid nesting them. First, you will take a look at the naive approach to modeling chained HTTP requests, which is using nested calls to fetch. This will be followed up with some optimizations that include Promise chaining and use of the async and await keywords.

#api #javascript

Handling Nested HTTP Requests Using the Fetch API
4.75 GEEK