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

136
Views
How to map a result in a promise that comes from a function call

I have this function that returns a result from a GET in an API (I am using an AWS Rest API, for contextualization):

const listUsers = async () => {
  const apiName = 'AdminQueries'
  const path = '/listUsers'
  const params = {
    headers: {
      'Content-Type': 'application/json',
      Authorization: `${(await Auth.currentSession())
        .getAccessToken()
        .getJwtToken()}`,
    },
  }
  return await API.get(apiName, path, params)
}

And if i call the function to print on console, like:

console.log(listUsers())

I get an array on my console:

Promise {<pending>}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Object
Users: Array(42)
0: {Username: "00e534e3-08ee-4bc1-9d19-ab18208e3f94", Attributes: Array(5), UserCreateDate: "2021-09-06T10:05:35.537Z", UserLastModifiedDate: "2021-09-06T10:05:35.537Z", Enabled: true, …}
1: {Username: "05f4ce2e-4b57-48d5-9f5f-e18d250aefb4", Attributes: Array(6), UserCreateDate: "2021-09-10T19:45:32.002Z", UserLastModifiedDate: "2021-09-10T19:54:37.333Z", Enabled: true, …}
2: {Username: "06a78cc7-af6a-4fb6-975d-458c089b69a5", Attributes: Array(6), UserCreateDate: "2021-09-10T18:04:27.780Z", UserLastModifiedDate: "2021-09-10T18:04:27.780Z", Enabled: true, …}
3: {Username: "32652d77-e7a4-444a-9995-f8f50eb14633", Attributes: Array(6), UserCreateDate: "2021-09-06T15:48:24.897Z", UserLastModifiedDate: "2021-09-06T15:49:39.476Z", Enabled: true, …}
length: 42
__proto__: Array(0)
__proto__: Object

How do i map these users to show in an array? I tried but couldn't.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your listUsers function is returning a promise. To get the response you can make use of something like this

const listUsers = async () => {
  const apiName = 'AdminQueries'
  const path = '/listUsers'
  const params = {
    headers: {
      'Content-Type': 'application/json',
      Authorization: `${(await Auth.currentSession())
        .getAccessToken()
        .getJwtToken()}`,
    },
  }
  return await API.get(apiName, path, params)
}

**** update ****
const func = async () => {
   const response = await listUsers();
   console.log(response);
}

func();
about 4 years ago · Juan Pablo Isaza Report

0

const listUsers = async () => {
      const apiName = 'AdminQueries'
      const path = '/listUsers'
      const params = {
        headers: {
          'Content-Type': 'application/json',
          Authorization: `${(await Auth.currentSession())
            .getAccessToken()
            .getJwtToken()}`,
        },
      }
      return await API.get(apiName, path, params)
    }

    const func = async () => {
      const response = await listUsers();
      reponse.data?.map((item) => {
        console.log(item);
      })
    }
about 4 years ago · Juan Pablo Isaza Report
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!