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

289
Visualizações
How to fetch multiple GET URLs at once
fetch(`./User.json`, { method: "get" })
.then(response => response.json())
.then(data =>
    data.results.forEach(result => (list.innerHTML += template(result)))
)
.catch(error => console.log(error));

I want to add some json data. what's the solution. That's my code.

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

0

Assuming you want all requests to complete, regardless of their response status, then Promise.allSettled() is an excellent choice

// handy "get" shortcut with error handling
const get = async (url) => {
  const res = await fetch(url) // "GET" is the default method
  if (!res.ok) {
    throw new Error(`${res.status}: ${await res.text()}`)
  }
  return res.json()
}

const urls = ["./Foo.json", "./Bar.json", "./Baz.json"]

Promise.allSettled(urls.map(get)).then(responses => {
  const data = responses
    .filter(({ status }) => status === "fulfilled")
    .map(({ value }) => value)
})
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Promise.all method to get the values of the fetch calls once all the promises have resolved, you'd get the values in an array which you can destruct as shown below

    Promise.all([fetch(`./User.json`, { method: "get" }).then(response => response.json())]), fetch(`./Products.json`, { method: "get" }).then(response => response.json())]).then([users, products]=>
    users.results.forEach(result => (list.innerHTML += template(result)))
)

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