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

142
Views
Why can't I single-line object properties from objects returned from async functions?

I am wanting to use an object that is returned from an asynchronous function.

async function myPromise() {
  console.log("Single-line", await getUsers().quote);
  let users = await getUsers();
  console.log("Multi-line", users.quote);
}

async function getUsers() {
  return await new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve({
        name: "Jeff",
        id: 1,
        quote: "Hello world!"
      });
    }, 1000)
  });
}

myPromise();

Why do I have to assign the result of an asynchronous function to a variable before I can use the properties stored within the object that was returned?

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

0

The property accessor has precedence over the await operator, so you need to add parentheses:

(await getUsers()).quote
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!