0% found this document useful (0 votes)
11 views4 pages

Ed

This function fetches data from an API, maps over the results to fetch additional nested data on species, vehicles, films, and starships for each item, and returns the final enriched data. It makes multiple asynchronous requests to external APIs to enrich the initial data with additional details on related entities for each result.

Uploaded by

c78947298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Ed

This function fetches data from an API, maps over the results to fetch additional nested data on species, vehicles, films, and starships for each item, and returns the final enriched data. It makes multiple asynchronous requests to external APIs to enrich the initial data with additional details on related entities for each result.

Uploaded by

c78947298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

const fetchData = async (value) => {

const response = await fetch(value);

const data = await response.json();

const characterWithSpecies = await Promise.all(

data.results.map(async (person, index) => {

if (person.species.length) {

const response = await (await fetch(person.species)).json();

return {

...person,

species: response.name,

id: index + 1,

};

return { ...person, species: "", id: index + 1 };

})

);

const charactersWithVehicles = await Promise.all(

characterWithSpecies.map(async (person) => {

if (person.vehicles.length) {

const vehicleResponse = await Promise.all(

person.vehicles.map(async (vehicle) => {

const data = await (await fetch(vehicle)).json();


return data;

})

);

return {

...person,

vehicles: vehicleResponse,

};

return {

...person,

vehicles: "",

};

})

);

const CharacterFilmoGraphy = await Promise.all(

charactersWithVehicles.map(async (person) => {

if (person.films.length) {

const filmsResponse = await Promise.all(

person.films.map(async (filmUrl) => {

const data = await (await fetch(filmUrl)).json();

return data.title;

})
);

return {

...person,

films: filmsResponse,

};

return {

...person,

films: "",

};

})

);

// Fetching films data for each person

const characterWithStarShips = await Promise.all(

CharacterFilmoGraphy.map(async (person) => {

if (person.starships.length) {

const response = await Promise.all(

person.starships.map(async (starShip) => {

const data = await (await fetch(starShip)).json();

return data;

})

);
return {

...person,

starships: response,

};

return {

...person,

starships: "",

};

})

);

return characterWithStarShips;

};

(async () => {

try {

const result = await fetchData("https://swapi.dev/api/people/");

console.log(result);

} catch (error) {

})();

You might also like