How to make axios synchronous

后端 未结 3 1136
情深已故
情深已故 2020-12-14 07:00

I\'m using axios to check if an alias has not already been used by another in the database.

Problem: The ajax call doesn\'t wait for the server resp

3条回答
  •  生来不讨喜
    2020-12-14 07:27

    If you just do what JS docs (mozilla) show you can tread Axios as just another promise. Be careful about making the request synchronous bc it can freeze the UI and the rest of your app.

           async save () {
                this.valid = true;
                console.log('before checking');
    
                const isUnique = await this.checkUniqueness();
                console.log(isUnique); // => value waited for and returned from this.checkUniqueness()
                // other validations here
    
                if (this.valid) {
                    console.log('3. checked valid, can save now');
                    // save now
                }
            }
    

提交回复
热议问题