How to turn an asynchronous function into synchronous?

Asked

Viewed 735 times

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

1 answer

7


Whereas arquivo.on('message' calls the function N times, 1 by email, so you can create Promises and insert into an array and then when arquivo.on('end' called you can expect all the files to be ready and use these mails.

The code would be like this:

imap.search(['ALL'], function(err, results) {
    if (err) throw err;
    let arquivo = imap.fetch(results, {
        bodies: ''
    });
    const emails = []; // <--- aqui vais juntando Promises
    arquivo.on('message', function(msg, num) {
        const compilador = new Promise((resolve, reject) => {
            msg.on('body', function(stream, info) {
                simpleParser(stream)
                    .then(mail => {
                        resolve({
                            id: num,
                            remetente: mail.from.text,
                            destinatario: mail.to.text,
                            assunto: mail.subject,
                            texto: mail.text
                        })
                    })
                    .catch(reject);
            });
            msg.on('end', function() {
                console.log(num + ' concluído!');
            })
        });
        emails.push(compilador);
    });
    arquivo.on('error', function(err) {
        console.log('Erro em arquivo.once: ' + err)
    });
    arquivo.on('end', function() {
        Promise.all(emails).then(array => { // <--- aqui vais vai verificar/esperar que todas estão prontas
            console.log(array);
            console.log('Concluído!');
        })
    })
})
  • 1

    It worked perfectly... Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.