Promise按顺序执行

mounted() {
	// 1、初始化页面用到的数据
	this._initData();
},
methods:{
	async _initData() {
		await this.getdata1();
		await this.getdata2();
		await this.getdata3();
	},
	getdata1() {
		return new Promise((req, rej) => {
			this.axios.post('/Market/areaList').then(res => {
				if (res.code == 1) {
					console.log(1)
				}
				req(res)
			})
		})
	},
	getdata2() {
		return new Promise((req, rej) => {
			this.axios.post('/Market/areaList').then(res => {
				if (res.code == 1) {
					console.log(2)
				}
				req(res)
			})
		})
	},
	getdata3() {
		return new Promise((req, rej) => {
			this.axios.post('/Market/areaList').then(res => {
				if (res.code == 1) {
					console.log(3)
				}
				req(res)
			})
		})
	},
}

最后输出顺序:1-2-3

你可能感兴趣的:(javascript)