I have an asynchronous function and would like it to become synchronous, as being asynchronous it is sending the data to the client before even completing the necessary steps, it 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 to make this function asynchronous ?? What is the correct place to put the response ??
Note: HapiJS Server