一、传统的ES6 : Promise请求,使用.then和.catch捕获
ganswitchApi() {
return new Promise((resolve, reject) => {
AppletConfigurationAPI.GameServerList({
game_id: this.gameid,
pageNot: true,
}).then((res) => {
resolve(res.data.data);
}).catch((error) => {
reject(error);
});
}).then((data) => {
this.gameswitchList = data;
// this.getNewRoleSwitch()
}).catch((error) => {
console.error(error);
});
},
二、案例
new Promise((resolve,reject) => {
setTimeout(() => {
resolve('hello')
}, 2000)
}).then(res => {
console.log(res)
}).catch((error)=>{
console.log(error);
})
三、 Promise使用 async 和 await
async ganswitchApi() {
const res = await AppletConfigurationAPI.GameServerList({
game_id: this.gameid,
pageNot: true,
});
if(res.data.code === 0){
this.gameswitchList = res.data.data;
}else{
console.log(error);
}
},