【发布时间】:2023-03-31 21:33:01
【问题描述】:
我是 Promise 的新手,在掌握如何将两个 Promise 组合在一起时遇到了一些麻烦。我有一个函数可以解决一个承诺并执行一些任务:
loginService (context, credentials) {
context.$http.post(LOGIN_URL, credentials, { emulateJSON: true}).then(response => {
this.user.authenticated = true
localStorage.setItem(TOKEN_STORAGE_NAME, response.body[TOKEN_NAME])
}).catch(response => {
context.error = response.body
})
},
我想修改上面的代码,以便我可以执行以下操作:
login () {
this.submitButtonLoading = true
loginService(this, this.credentials).then(response => {
this.submitButtonLoading = false
// Do something else here.
}).catch(response => {
this.submitButtonLoading = false
// Do something else here.
})
}
处理这个问题的正确方法是什么?
【问题讨论】:
-
你的函数需要
return语句。
标签: javascript promise es6-promise vuejs2 vue-resource