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

167
Views
Estoy usando async, await, Promise en JavaScript pero los resultados no se envían a la respuesta en Express.js

Soy nuevo en nodejs (Express.js y JavaScript), así que discúlpeme. Tengo el siguiente código

 function lookUpServiceProcessor(element){ return new Promise((resolve, reject) => { const lookUpService = new LookUpService(); const data = lookUpService.process(element); resolve(data); reject(data); }) } function keywordServiceProcessor(element){ return new Promise((resolve, reject) => { const keywordService = new KeywordService(process.env.KEYWORD_SERVICE_HOSTNAME, process.env.KEYWORD_SERVICE_PORT, process.env.KEYWORD_SERVICE_ENDPOINT); const data = keywordService.process(element); resolve(data); reject(data); }) } function patternServiceProcessor(element){ return new Promise((resolve, reject) => { const patternService = new PatternService(process.env.PATTERN_SERVICE_HOSTNAME, process.env.PATTERN_SERVICE_PORT, process.env.PATTERN_SERVICE_ENDPOINT); const data = patternService.process(element); resolve(data); reject(data); }) } const processEachDocument = (document) => { return Promise.all([ lookUpServiceProcessor(document), keywordServiceProcessor(document), patternServiceProcessor(document) ]); } router.post('/extraction', (req, res) => { result = []; req.body.map(document => { processEachDocument(document) .then(response => result.push(response)) .catch(error => result.push(error)); }); res.send(result); })

Problema :

  1. No se rellena nada en la matriz de resultados . Entiendo que nodejs activa un subproceso en segundo plano (es decir, como programación reactiva) para procesar Promise y necesitamos async y esperar a que aparezcan los resultados.
  2. Por lo tanto, probé el siguiente código con async y await, pero la matriz de resultados no tiene nada.
  3. En ambos casos, cuando consola.log, se completan. Leí un par de publicaciones de stackoverflow y entiendo que necesito tener el asíncrono y esperar en los lugares adecuados. Como soy nuevo, no estoy llegando a donde necesito que me envíen la respuesta adecuada.

Cualquier pista es muy apreciada. Gracias

 router.post('/extraction', (req, res) => { result = []; req.body.map(document => { (async () => { const processedDocument = await processEachDocument(document); await result.push(processedDocument); })(); }); res.send(result); })
about 4 years ago · Juan Pablo Isaza
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!