From 96f5d89e701170deffa362c78056c17c68de8a9c Mon Sep 17 00:00:00 2001
From: Kobe Osei-Akoto 7.1 : Asynchronous Programming Basics
Notes:
then()
let stateOfLoading = 'Not Loading'; // this is the default state
-fetch('https://swapi.co/api/peoples/') // breaks because the URL is people not peoples
+fetch('https://swapi.dev/api/peoples/') // breaks because the URL is people not peoples
.then((response) => {
if(!response.ok){ // run if fetch fails
throw Error('api call broke'); // catch() is called
@@ -4129,25 +4129,25 @@ 8.5 : The await
operator is sequential
(async () => { // create async function that is immediately invoked
// sequential fetch's
- const responsePlants = await fetch('https://swapi.co/api/planets');
+ const responsePlants = await fetch('https://swapi.dev/api/planets/');
// API call above has to finish before the API below can start
- const responseFilms = await fetch('https://swapi.co/api/films');
+ const responseFilms = await fetch('https://swapi.dev/api/films/');
console.log(responsePlants.ok, responseFilms.ok) // logs true true
// parallel fetch's, faster than sequential fetch's using Promise.all()
const [parallelResponsePlanets, parallelResponseFilms] = await Promise.all([
- fetch('https://swapi.co/api/planets'),
- fetch('https://swapi.co/api/films')
+ fetch('https://swapi.dev/api/planets/'),
+ fetch('https://swapi.dev/api/films/')
]);
console.log(parallelResponsePlanets.ok, parallelResponseFilms.ok) // logs true true
// parallel fetch's, return first one to finish, using Promise.race()
const fastestResponseFromStarWarsApi = await Promise.race([
- fetch('https://swapi.co/api/species'),
- fetch('https://swapi.co/api/vehicles'),
- fetch('https://swapi.co/api/starships')
+ fetch('https://swapi.dev/api/species/'),
+ fetch('https://swapi.dev/api/vehicles/'),
+ fetch('https://swapi.dev/api/starships/')
]);
console.log(fastestResponseFromStarWarsApi.ok); // logs true