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

182
Views
Expresar js asíncrono

Cuando mi código es así, la representación es temprana. Quiero que los procesos anteriores se ejecuten primero y luego el proceso de renderizado (promesa). ¿Qué debo hacer para solucionar el problema?

 app.get('/inbox', (req, res) => { var messagesList = { messages: [], subjects: [], dates: [], from: [] }; imaps.connect(config).then(function(connection) { connection.openBox('INBOX', (err, box) => { console.log(box.messages) res.locals.box_messages = box.messages; var searchCriteria = ['1:' + box.messages.total]; var fetchOptions = { bodies: ['HEADER', 'TEXT', ''], }; connection.search(searchCriteria, fetchOptions).then(function(messages) { messages.forEach(function(item) { var all = _.find(item.parts, { "which": "" }) var id = item.attributes.uid; var idHeader = "Imap-Id: " + id + "\r\n"; simpleParser(idHeader + all.body, (err, mail) => { messagesList.messages.push(mail.html) messagesList.subjects.push(mail.subject) messagesList.dates.push(mail.date) messagesList.messages.push(mail.from.text) res.locals.messagesList = messagesList }) }) }) }) }) res.render('index') })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Debe cambiar forEach para un mapa y hacer que simpleParser sea asíncrono para que espere a que termine antes de renderizar. Prueba este código:

 app.get("/inbox", (req, res) => { imaps.connect(config).then(function (connection) { connection.openBox("INBOX", (err, box) => { console.log(box.messages); res.locals.box_messages = box.messages; var searchCriteria = ["1:" + box.messages.total]; var fetchOptions = { bodies: ["HEADER", "TEXT", ""], }; connection.search(searchCriteria, fetchOptions).then(async function (messages) { var messagesList = { messages: [], subjects: [], dates: [], from: [], }; for(let i = 0; i < messages.length; i++) { var all = _.find(item.parts, { which: "" }); var id = item.attributes.uid; var idHeader = "Imap-Id: " + id + "\r\n"; try { await new Promise(function (resolve, reject) { simpleParser(idHeader + all.body, (err, mail) => { if (err) reject(err); messagesList.messages.push(mail.html); messagesList.subjects.push(mail.subject); messagesList.dates.push(mail.date); messagesList.messages.push(mail.from.text); res.locals.messagesList = messagesList; resolve(mail); }); }); } catch (e) { // handle error } }; res.render("index"); }); }); }); });
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!