Search code examples
javascriptpromisees6-promiseresolve

How to resolve a promise from another function


I don't get how to resolve a promise from an other function.

How can 1. tell to blabla() 'mail received' by sending the mail?

  1. This email module manage the mails, it fire 'new' when we receive an email.

mail.on('new', function(mail) {})

  1. This module do stuff and at some point must wait for the_mail, before continuing.

    function blabla() {
        [.. I do stuff ..]
        console.log('now we must wait for a new mail')
        const the_mail = await waiting_for_mail()
        [.. I can continue stuff (I have the mail) ..]
    }
    
    • The two modules must be separated, they perform != tasks.
    • The first manage the mails, the second manage SMS.
    • waiting_for_mail() isn't defined, I guess that I have to use this logic.

Solution

  • Well, I have very little to go on here, but here's a suggestion from what I could gather:

    function waiting_for_mail() {
        return new Promise(resolve => {
            mail.once('new', resolve);
        });
    }