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

110
Views
usando await dentro de la función de backend api

Tengo el código a continuación:

servicio.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()); } }); }

controlador.js

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

Esto arroja un error: SyntaxError: await is only valid in async functions and the top level bodies of modules . ¿Cómo puedo recuperar los datos de la espera de manera efectiva?

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

0

Puede usar await solo dentro de la función async .

 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!