Skip to main content
se añadió 1 carácter en el cuerpo
Origen Enlace
Pablo Lozano
  • 49.9k
  • 8
  • 55
  • 92

Puedes usar otro Promise.all() si las promesas retornan promesas:

Promise.all(resultado).then(res=>console.log(res));

//const axios = require("axios");
const rootApi = "https://swapi.codev/api";

const main = async () => {
  const people = [];
  var next = `${rootApi}/people`;
  do {
    const axiosRequest = await axios.get(next);
    const dataPeople = axiosRequest.data.results;
    people.push(dataPeople);
    next = axiosRequest.data.next;
    console.log("va a tardar..");
  } while (next !== null);

  const peopleFlat = [].concat(...people);
  const resultado = peopleFlat.map(async p => {
    let promisesArrFilms = p.films.map(film => axios.get(film));
    const obj = {};
    obj.name = p.name;
    obj.height = p.height;
    obj.mass = p.mass;
    obj.hair_color = p.hair_color;
    obj.skin_color = p.skin_color;
    obj.eye_color = p.eye_color;
    obj.birth_year = p.birth_year;
    obj.gender = p.gender;
    obj.films = await Promise.all(promisesArrFilms);
    return obj;
  });

Promise.all(resultado).then(res=>console.log(res));
 
};

main();
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

Puedes usar otro Promise.all() si las promesas retornan promesas:

Promise.all(resultado).then(res=>console.log(res));

//const axios = require("axios");
const rootApi = "https://swapi.co/api";

const main = async () => {
  const people = [];
  var next = `${rootApi}/people`;
  do {
    const axiosRequest = await axios.get(next);
    const dataPeople = axiosRequest.data.results;
    people.push(dataPeople);
    next = axiosRequest.data.next;
    console.log("va a tardar..");
  } while (next !== null);

  const peopleFlat = [].concat(...people);
  const resultado = peopleFlat.map(async p => {
    let promisesArrFilms = p.films.map(film => axios.get(film));
    const obj = {};
    obj.name = p.name;
    obj.height = p.height;
    obj.mass = p.mass;
    obj.hair_color = p.hair_color;
    obj.skin_color = p.skin_color;
    obj.eye_color = p.eye_color;
    obj.birth_year = p.birth_year;
    obj.gender = p.gender;
    obj.films = await Promise.all(promisesArrFilms);
    return obj;
  });

Promise.all(resultado).then(res=>console.log(res));
 
};

main();
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

Puedes usar otro Promise.all() si las promesas retornan promesas:

Promise.all(resultado).then(res=>console.log(res));

//const axios = require("axios");
const rootApi = "https://swapi.dev/api";

const main = async () => {
  const people = [];
  var next = `${rootApi}/people`;
  do {
    const axiosRequest = await axios.get(next);
    const dataPeople = axiosRequest.data.results;
    people.push(dataPeople);
    next = axiosRequest.data.next;
    console.log("va a tardar..");
  } while (next !== null);

  const peopleFlat = [].concat(...people);
  const resultado = peopleFlat.map(async p => {
    let promisesArrFilms = p.films.map(film => axios.get(film));
    const obj = {};
    obj.name = p.name;
    obj.height = p.height;
    obj.mass = p.mass;
    obj.hair_color = p.hair_color;
    obj.skin_color = p.skin_color;
    obj.eye_color = p.eye_color;
    obj.birth_year = p.birth_year;
    obj.gender = p.gender;
    obj.films = await Promise.all(promisesArrFilms);
    return obj;
  });

Promise.all(resultado).then(res=>console.log(res));
 
};

main();
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

Origen Enlace
Emeeus
  • 7.4k
  • 11
  • 25

Puedes usar otro Promise.all() si las promesas retornan promesas:

Promise.all(resultado).then(res=>console.log(res));

//const axios = require("axios");
const rootApi = "https://swapi.co/api";

const main = async () => {
  const people = [];
  var next = `${rootApi}/people`;
  do {
    const axiosRequest = await axios.get(next);
    const dataPeople = axiosRequest.data.results;
    people.push(dataPeople);
    next = axiosRequest.data.next;
    console.log("va a tardar..");
  } while (next !== null);

  const peopleFlat = [].concat(...people);
  const resultado = peopleFlat.map(async p => {
    let promisesArrFilms = p.films.map(film => axios.get(film));
    const obj = {};
    obj.name = p.name;
    obj.height = p.height;
    obj.mass = p.mass;
    obj.hair_color = p.hair_color;
    obj.skin_color = p.skin_color;
    obj.eye_color = p.eye_color;
    obj.birth_year = p.birth_year;
    obj.gender = p.gender;
    obj.films = await Promise.all(promisesArrFilms);
    return obj;
  });

Promise.all(resultado).then(res=>console.log(res));
 
};

main();
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>