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

101
Views
using await within function of backend api

I have the code below:

service.js

module.exports = {
  getUser
};
async function getUser({ data }) {

  return new Promise((resolve, reject) => {

    const id = data["id"];
  
    const doc = await db.collection('users').where('id', '==', id).get();

    if (!doc.exists) {
      resolve('No such document!');
    } else {
      resolve(doc.data());
    }

  });

}

controller.js

async function getUser(req, res, next) {
  userService.getUser({ data: req.body }).then(function (val) {
    res.json(val);
  });
}

This throws an error: SyntaxError: await is only valid in async functions and the top level bodies of modules. How can I retrieve the data from the await in an effective manner?

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

0

You can use await only inside async function.

function dummy() {
  return new Promise((res, rej) => {
    setTimeout(() => {
      res(20)
    }, 300)
  })
}

let val = new Promise(async(resolve, reject) => {
  let v = await dummy()
  resolve(v)
})


val.then(value => console.log(value))

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!