Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Async + Resolve Within If Statement

I’m wondering if there’s a way to clean this up or if I need to put a separate resolve() within each if/else like it is now. When I put it outside the three it says message is not defined.

function checkAddUser(input) {

    return new Promise((resolve,reject) =>{

        if(input.email === '') {

            const message = 'Please enter an email address.';

            resolve(message);

        } else if (input.first_name === '') {

            const message = 'Please enter your first name.';

            resolve(message);

        } else {

            const message = false;

            resolve(message);

        }

    });

}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There is no need to return a promise, since no asynchronous calls are made. To clean up the code I’d suggest this:

function checkAddUser(input) {
        if(input.email === '') {
        
            return 'Please enter an email address.';
            
        }
        
        if (input.first_name === '') {
        
            return 'Please enter your first name.';
            
        }
        
        return false;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading