Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda