本文主要是介绍【前端】async+await 控制代码执行顺序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景:
B请求要用到A请求的返回的数据,要求A请求先执行,B请求后执行
解决:
(1)示例一
created() {Promise.all([this.getBondListData(), this.getBasicConditionsData(), this.getReleaseInformationData()]).then((result) => {this.getPaymentInformationData();this.getCapitalConditionData();this.getCreditEvaluationData();}).catch((error) => {this.getPaymentInformationData();this.getCapitalConditionData();this.getCreditEvaluationData();})}
(2)示例二
mounted() {this.start();},methods:{async start() {if (this.permission() == true) {...let code = await this.shift();await this.requestData(code);} else {...}},//先执行shift() {return new Promise((resolve, reject) => {EMAjax.get({url: '...',data: this.data,success: (data) => {if (data && data.list && data.list.length) {resolve(data.list[0].code);} else {resolve("");}},error: (err) => {resolve("");},});});},//后执行requestData(code) {...}
}
这篇关于【前端】async+await 控制代码执行顺序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!