6
I have an asynchronous function and I would like it to become synchronous, because being asynchronous she is sending the data to the client before even completing the necessary steps, follows code:
imap.once('ready', function () {
openInbox(function (err, box) {
if (err) throw err;
imap.search(['ALL'], function (err, results) {
if (err) throw err;
let arquivo = imap.fetch(results, {bodies: ''});
arquivo.on('message', function (msg, num) {
msg.on('body', function (stream, info) {
simpleParser(stream)
.then(mail => {
email = {
id: num,
remetente: mail.from.text,
destinatario: mail.to.text,
assunto: mail.subject,
texto: mail.text
};
})
.catch(err => {
console.log(err)
});
});
msg.on('end', function () {
console.log(num + ' concluído!');
})
});
arquivo.on('error', function (err) {
console.log('Erro em arquivo.once: ' + err)
});
arquivo.on('end', function () {
console.log('Concluído!')
})
})
})
});
imap.once('error', function (err) {
console.log('Erro no Imap.once' + err);
});
imap.once('end', function () {
console.log('Encerrado!');
});
imap.connect();
How do I make this function asynchronous?? What is the right place to put Sponse??
Note: Hapijs Server
It worked perfectly... Thank you very much
– LeonardoEbert