Javascript 异步和等待不工作

Javascript 异步和等待不工作,javascript,vue.js,async-await,Javascript,Vue.js,Async Await,它不是等待验证,而是运行else部分:|我的错误在哪里 async validateBeforeSubmit(event) { await this.$validator.validateAll().then( result => { if (result) { console.log(result); // just log the -> true // go subm

它不是等待验证,而是运行else部分:|我的错误在哪里

  async validateBeforeSubmit(event) {
        await  this.$validator.validateAll().then(  result => {
            if (result) {
                 console.log(result); // just log the -> true
                // go submit
            }else{
                console.log(result);  // just log the -> false
                event.preventDefault();
                var elmnt = document.getElementById("drop_zone");
                elmnt.scrollIntoView();
            }
        })
        .catch(error=>console.log(error));
    },
我正在使用veevalidator,我定义了一些需要几秒钟才能解决的自定义规则:


 created() {
        this.$validator.extend('unique', {
            //   getMessage: field => 'At least one ' + field + ' needs to be checked.',
            async validate(value, arg) {
                arg = arg[0];
                let sw = false;
                if (arg == 'n_code') {
                    let data = {
                        'n_code': value
                    }
                    await Axios.post(duplicate_ncode, data, {
                        headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content') }
                    })
                        .then((response) => {
                            if (response.data == true) {
                                sw = true;
                            }
                        })
                        .catch(error => console.log(error));
                    if (sw) {
                        return true;
                    } else {
                        return false;
                    }

                }
                if (arg == 'email') {
                    let data = {
                        'email': value
                    }
                    await Axios.post(duplicate_email, data, {
                        headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content') }
                    })
                        .then((response) => {
                            if (response.data == true) {
                                sw = true;
                            }
                        })
                        .catch(error => console.log(error));
                    if (sw) {
                        return true;
                    } else {
                        return false;
                    }

                }
                if (arg == 'mobile') {
                    let data = {
                        'mobile': value
                    }
                    await Axios.post(duplicate_mobile, data, {
                        headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content') }
                    })
                        .then((response) => {
                            if (response.data == true) {
                                sw = true;
                            }
                        })
                        .catch(error => console.log(error));
                    if (sw) {
                        return true;
                    } else {
                        return false;
                    }

                }
                // console.log('questions', value, testProp, options.some((option) => option[testProp]));
                // return true;
            }
        });
    }

当用户填写所有字段时,它将检查3个api和需要检查的MOMNET。 我需要等待答案,但有一点是错误的,这是不起作用的


请帮忙

我相信你想做的是:

async validateBeforeSubmit(event) {
  try {
    const result = await this.$validator.validateAll();
    if (result) {
      // go submit
    } else {
      event.preventDefault();
      var elmnt = document.getElementById('drop_zone');
      elmnt.scrollIntoView();
    }
  }
  catch (e) {
    console.log(e);
  }
},

您正在使用两种不同的模式。异步和等待以及事件链接回调。这是什么。$validator?如果它真的返回了一个承诺,那么这段代码应该等待承诺所代表的内容完成后再继续。正如@CodeMonkeyForHire所指出的,它不是惯用的语言,但您上面所说的应该是有效的。假设ValidateBeforSubmit永远不允许其承诺被拒绝(就像上面的代码一样),那么习惯用法就是FWIW。如果您现在运行此操作,控制台中显示的实际消息是什么?表单将提交,并且不显示任何消息,因为它不会等待响应,但如果我使用event.preventDefault;在顶部停止提交,它将在3秒钟后显示验证结果,如果验证为真,则为真,如果验证为假,则为假。可能我在其他地方有错误,我在表单标记上的'v-on:submit=validateBeforeSubmit'上运行此函数。请添加一个console.logresult并在您的问题中添加输出。我将其添加到答案中,并提供了一些更详细的信息