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

213
Visualizações
How to return a value from a fetch function

I'm looping through an array of posts. Each post has a unique user id associated with it and im trying to take that id and call another API and return an image URL.

I've created this function that should return the url

const getUser = async (id) => {
  let url = `http://127.0.0.1:8000/api/user/${id}/`
  const response = await fetch(url)
  const data = await response.json()
  const returnedurl = data.avatar
  return returnedurl
}

and then I put it in an img tag with the id of the current post.

{data?.map((data) => (
  <div className="allpostsingle" key={data.id} onClick={() => sendTo(data.id)}>
      <div className="allpostheader">
          <img src={getUser} />
          <p>Asiqur</p>
      </div>
      <div className="allpostimages">
          <div className="allpostdiv">
              <p>{data.wanted}</p>
              <img src={data.wanted_image} />
          </div>
      </div>
  </div>
))}         

The issue is that instead of returning a url it returns a promise. So I've tried creating another function to resolve the promise like so

const getUser = async (id) => {
  let url = `http://127.0.0.1:8000/api/user/${id}/`
  const response = await fetch(url)
  const data = await response.json()
  const returnedurl = data.avatar
  getting(returnedurl)
}

const getting = async (url) => {
  const imageurl = await url
  return imageurl
}

but it still returns a promise. How can I solve this, or is there a better way of approaching this.

about 4 years ago · Santiago Gelvez
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