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

162
Views
Cannot have multiple awaits in the same async function?
function fakeRequest(url) {
    return new Promise((resolve, reject) => {
        delay = Math.floor(Math.random() * 4500) + 500;
        setTimeout(() => {
            if (delay > 4500) {
                resolve(url + ": success")
            }
            else {
                reject(url = ": error")
            }
        }, delay);
    })
  })
}

async function makeTwoRequests() {
    let data1 = await fakeRequest("/page1");
    console.log("Data 1:", data1)
    let data2 = await fakeRequest("/page2");
    console.log("Data 2:", data2)
}
makeTwoRequests()


When I remove data1 or data2 and just have 1 await it works. But when there are 2 or more it errors out saying: Uncaught (in promise) : error I don't know what's happening here. Please help me out

Thanks!

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

0

If you await a promise that rejects, it becomes a thrown error.

Change the reject inside fakeRequest to a resolve and I bet the problem goes away.

This works for me:

function fakeRequest(url) {
  return new Promise((resolve, reject) => {
    let delay = Math.floor(Math.random() * 4500) + 500;
    setTimeout(() => {
      if (delay > 4500) {
        resolve(url + ": success")
      } else {
        resolve(url + ": error")
      }
    }, delay);
  })
}

async function makeTwoRequests() {
  let data1 = await fakeRequest("/page1");
  console.log("Data 1:", data1)
  let data2 = await fakeRequest("/page2");
  console.log("Data 2:", data2)
}

makeTwoRequests()

There were a couple other issues with the sample code:

  • delay ought to be declared with a keyword like let, const, or var; this is necessary to guarantee that each invocation of the function uses its own private version instead of a shared one, and to prevent this function from changing variables in the outer scope
  • the reject call uses = instead of +, which redefines and returns the url variable instead of concatenating the URL string with the literal : error string
  • there was an extra closing }), which prevented this code from parsing

But I think I get what you're up to: you're trying to simulate a situation in which API calls usually succeed, except for some that fail by timing out.

Your fakeRequest does accomplish that, but your calling code isn't capable of handling the problem. For that, you'd need something like this:

async function makeTwoRequests() {
  let data1
  try {
    data1 = await fakeRequest("/page1");
  } catch ( error ) {
    console.error(`request 1 threw`, error)
    // here, you might return, or re-throw, or fall through
  }
  
  let data2
  try {
    data2 = await fakeRequest("/page2");
  } catch ( error ) {
    console.error(`request 2 threw`, error)
    // here, you might return, or re-throw, or fall through
  }
}
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!