Home Javascript - async await doesn't work with conditional
Reply: 2

Javascript - async await doesn't work with conditional

Jaroslav KlimĨík
1#
Jaroslav KlimĨík Published in 2018-01-12 14:43:11Z

I've been working on it for several hours and I'm still more and more confused about how async/await works. Right now I have this code:

    created: function () {

         Event.$on('open-stage', (stage) => {

            this.$validator.validateAll().then(() => {

                const validateFields = async () => {
                    const uniqueEmail = await this.checkIfUniqueEmail();
                    if(uniqueEmail) {
                       console.log('uniqueEmail is true'); // <-- this is what I need to achieve
                       Event.$emit('change-stage', this.wizardStage.successor);
                    }
                };

                validateFields();
            }).catch(() => {
                toastr.warning('Error');
                Event.$emit('change-stage-denied');
                return true;
            });
        });
    },

    methods: {
        checkIfUniqueEmail() {
            if (!this.player.email || !this.player.email.length) {
                return true;
            }

            this.$http.post('playerExists', {
                email: this.player.email
            }).then(response => {
                if (response.data.exists) {
                    toastr.error(Good');
                    Event.$emit('change-stage-denied');
                    return false;
                }
                return true;
            }).catch(error => {
                toastr.warning('Fail');
                Event.$emit('change-stage-denied');
                return false;
            });
        },
    }

My goal is simple - if method checkIfUniqueEmail() returns true I want to see console.log and will emit change-state. The problem is that constant uniqueEmail is always undefined. How can I make this only after response from function checkIfUniqueEmail() is true? What I need to change? I'm using vue.js 2.1.10.

Giacomo Cosimato
2#
Giacomo Cosimato Reply to 2018-01-12 18:57:32Z

You need to make your method return the promise

checkIfUniqueEmail() {
    if (!this.player.email || !this.player.email.length) {
        return true;
    }

    return this.$http.post('playerExists', {
        email: this.player.email
    }).then(response => {
        if (response.data.exists) {
            toastr.error('Good');
            Event.$emit('change-stage-denied');
            return false;
        }
        return true;
    }).catch(error => {
        toastr.warning('Fail');
        Event.$emit('change-stage-denied');
        return false;
    });
}
localStanger
3#
localStanger Reply to 2018-01-12 16:49:26Z

Consider this:

    async function validateFields() {
      var uniqueEmail = await checkIfUniqueEmail();
        if(uniqueEmail) {
          console.log('uniqueEmail is true'); 
        }  
    }
    
    function checkIfUniqueEmail(){
      return new Promise(resolve => {
      	setTimeout(() => {
          resolve('johny@bravo.com');
        }, 1000)
      })
    }
    
    validateFields();

The above, actually represents your situation, so considering this simple example it is clear that simply returning a resolved Promise is a possible way to go. So you can just do something like:

this.$http.post('playerExists', {
    email: this.player.email
}).then(response => {
    if (response.data.exists) {
        toastr.error('Good');
        Event.$emit('change-stage-denied');
        return new Promise(resolve => {
           resolve(true)
        };
    }
<...>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.26324 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO