Promise.all同時調用多個接口

菜鳥寫法

	for(let i=0,l=list.length;i<l;i++) {
		let data = {
			id: list[i].id
		}
		this.$axios_post(data,res=>{
			...
		})
	}
	

大鳥寫法

let arr = [];
for(let i=0,l=list.length;i<l;i++) {
	let data = {
		id: list[i].id
	}
	arr.push(this.get_code(data))
}
Promise.all(arr).then(values => { 
 	 this.$router.push({ path: '/index'});
     }, reason => {
    	 console.log(reason)
     }
});

get_code(data) {
	return new Promise((resolve, reject)=>{
		this.$axios_post(data,res=>{
			...
			resolve(res)
		})
	})
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章