Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
Manipulation with multiples fetch and then

im learning promises for the school and there a lot of things that i dont understand

async function getCompanion(name){
const response = await fetch("https://rickandmortyapi.com/api/character/?name=" + name)
if(response.status !== 200) throw `HTTP error: ${response.status}`;
const data =  response.json()
return data
.then((body) => {
  const [index] = body.results;
  const ceo = index.episode 
  let friends = [];
  friends.push(ceo);
  return fetchAll(friends);
   
    
})
.then((body2) => {
  const [index2] = body2;
  const ceo2 = index2.characters
  let friends2 = [];
  friends2.push(ceo2);
  //console.log(friends2)
  return fetchAll(friends2)
}).then((body3) =>{
  console.log(body3);
  
})

.catch( (error) => {
  console.log(`Error: ${error}`)
})
}



async function fetchAll(urls){
  const res = await Promise.all(urls.map(u => fetch(u)))
  const jsons = await Promise.all(res.map(r => r.json()))
  console.log(jsons)
  return jsons
  
}

What i want to do:

its a async function that when you pass a name you get the name of the companions that share a episode with the name used, my problem is that the first .then works like i want, i get the episodes where the character take part, but the second fetchAll dont work, and i dont know why, the idea is that the second fetchAll get the data of each character in friends2

i think my code is a mess, but im starting to learn promises, sorry if the code its horrible

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

.then((body) => {
  const [index] = body.results;
  const ceo = index.episode 
  let friends = [];
  friends.push(ceo);
  return fetchAll(friends);
    
})

Do this change and it should work. fetchAll() is a asynchronous function which returns promise, either you need to put await before it or just directly return it because anyway you are not using that characters constant.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda