Module 7 · Lesson 3 of 427/32 in the course~20 min
Module lessons (3/4)
fetch (simulated)
Lesson preview5 hands-on exercises readyJavaScript Course · Module 7 — Asynchronous
Call an endpoint, read JSON, compose results with Promise.all.
What you'll learn in this lesson
- Master double
awaitoffetch(url)->response.json()for HTTP JSON calls. - Always check
if (!response.ok)becausefetchdoes not reject the Promise on HTTP 404 or 500 statuses! - Launch independent requests in parallel with
Promise.allmaintaining the order of the input array. - Distinguish when to use
Promise.all(all necessary) vsPromise.race(first to respond).
Overview
fetch(url) returns a Promise<Response>. Parsing the body via response.json() is also asynchronous and returns a second Promise — which is why reading JSON data requires two await. In the exercises below you can practice on injected fetch without depending on the real network.
Sections covered in this lesson
- 1Mental model: looping a request
fetch+Promise.allvsrace - 2Progressive examples
- 3Guided laboratory
- 4Check for understanding
Pro Lesson · Module 7 — Asynchronous
Unlock "fetch (simulated)" and the full JavaScript Course curriculum
Call an endpoint, read JSON, compose results with Promise.all.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in JavaScript Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.