如何从.then()中返回非promise的数据呢?
有时候,我们调用了接口,想要从.then()中,将该接口的数据返回出来,供别的方法中使用,这时,我们可以使用async和await,如下:
async handleTempStorage() {
this.tempSaveData = this.tempSave();
return saveIncome(this.tempSaveData).then(res => {
if (res.success) {
this.$message({
message: res.message,
type: "success",
duration: 1000
});
return Promise.resolve(true); //此时将基本类型的数据true返回出去
}
});
},
async handleView() {
let res = await this.handleTempStorage(); //在这里使用,得到res的值为true
if(res) this.matrixesOpenShow();
},