Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在另一个承诺中解决承诺_Javascript_Promise_Es6 Promise_Vuejs2_Vue Resource - Fatal编程技术网

Javascript 在另一个承诺中解决承诺

Javascript 在另一个承诺中解决承诺,javascript,promise,es6-promise,vuejs2,vue-resource,Javascript,Promise,Es6 Promise,Vuejs2,Vue Resource,我对承诺不太熟悉,在理解如何将两个承诺结合在一起时有点困难。我有一个函数,它解决了一个承诺,并执行一些任务: loginService (context, credentials) { context.$http.post(LOGIN_URL, credentials, { emulateJSON: true}).then(response => { this.user.authenticated = true localStorage.setItem(TOKEN_ST

我对承诺不太熟悉,在理解如何将两个承诺结合在一起时有点困难。我有一个函数,它解决了一个承诺,并执行一些任务:

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.
  })
}

处理此问题的正确方法是什么?

loginService
返回承诺,然后您可以继续链接:

loginService (context, credentials) {
  return 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(_ => {
      this.submitButtonLoading = false
      // ...
    })
}

请注意,
login
中的
then()
函数将始终执行,无论调用是否成功。

要使loginService成为承诺,只需在函数内返回一个
承诺即可。确保在函数已满时调用
resolve
和/或
reject

在您的示例中,您可以:

var loginService = function(context, credentials) {
  return new Promise( (resolve, reject) => {
    context.$http.post(LOGIN_URL, credentials, {emlateJSON: true})
      .then(response => {
        if (success) {
          this.user.authenticated = true
          resolve(this.user)
        } else {
          reject(new Error('Log in failed'))
        }
      })
      .catch(err => {
        reject(err)
      })
    })
 }
现在它就可以了


请注意,整个函数实际上只是返回一个大承诺。还要注意成功或失败是如何解决或拒绝的。

您的函数需要
return
语句。太好了,谢谢!我已经试过了,但无法区分它是否成功。有没有办法处理它是否存在?@hatzipanis承诺链中任何地方抛出的未捕获错误将直接传播到
catch
处理程序(如果存在)。否则错误会被默默地吞没,所以请始终使用
catch
@JaredSmith谢谢!您是否可以发布一个示例?要修复在
登录
中仅调用then处理程序的问题,请在第一个catch处理程序中重新显示错误,以触发
登录
中的第二个catch。换句话说,尝试插入
抛出响应
after
context.error=response.body太棒了!这正是我想要的。非常感谢你。现在再进一步研究一下它的内部工作原理。该方法似乎已经返回了一个承诺,这将使它成为一种反模式。谢谢@Traktor53,我开始沿着这条路线走,并意识到它可能是。