Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!