0

I am trying to implement async/await module. In function2 if user is not present i want to stop the execution there . i dont want to execute function 3, if user present , i want to execute function3

    const function1() {
                 //code
                 return a
                }

    const function2= (a,email) => model.user.findOne({ email:email}).then((user) => {
                    if (!user) {
                        ****// want to stop execution here****
                    } else {
                        //update details of user
                    }

                    return dealer;
                });


 const function 3 = function(dealer) {
            //mail sending
   };

 exports.mailer = async(email,callback) => {
                try {
                    var a =await function1();
                    var dealer =await function2(a,email);  //stop execution here       
                    var res  = await function 3(dealer);
                        await callback(res);
                    }catch (err){
                      callback(err);
                    }
                }
2
  • When you say stop execution what do you mean? You can throw an error in your function2 inside the if(!user) statement and your catch statement in your export function will handle it. Commented Dec 20, 2017 at 6:58
  • i need a message user already registered, actually it is not a error, right? Commented Dec 20, 2017 at 7:07

1 Answer 1

2

Returning a new Promise in function2 which resolves if a user is found, and rejects if not should achieve what you want I believe. Something like this:

const function1 = () => {
    return a
}

const function2 = (a, email) => {
    return new Promise((resolve, reject) => {
        model.user.findOne({
            email: email
        }).then((user) => {
            if (!user) {
                // want to stop execution here****
                reject(new Error('Error Message'))
            } else {
                // update details of user
                resolve(dealer)
            }
        })
    })
}

const function3 = (dealer) => {
    //mail sending
}

exports.mailer = async (email, callback) => {
    try {
        var a = await function1()
        var dealer = await function2(a, email) // stop execution here
        var res = await function3(dealer)
        await callback(res)
    } catch (err) {
        callback(err)
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

it resolved my problem, but i have one doubt, async /await are used to replace promises, right?
Async await only works with promises. You can only await a promise. Async await can be used instead of .then and .catch, although I also wouldn't call it a replacement for those, just an alternative. In my current major project, I use a combination of await and .then, depending on the circumstance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.