【问题标题】:javascript promises onSuccess handlerjavascript 承诺 onSuccess 处理程序
【发布时间】:2014-11-08 19:01:41
【问题描述】:

是否有一个处理程序方法,与失败相对应,类似于成功,可以真正退出异步队列并继续正常的函数调用。

让我详细说明。比方说

getConnection()
.then(function(connection){
     return self.getRecords() //some async routine, returns promise, does reject/resolve
})
.then(function(data){
     return self.getDetail() //some async routine, returns promise, does reject/resolve
})
.then(function(data){ //I'm done here calling onResult, but this onResult may call several
     self.onResult(data); //other functions down the road resulting in .fail() call
}) //I want to get out of this promise queue and continue with normal functions calls
.fail(function(info){
     self.onFault(info); //any error onFault causes down the road shouldn't be q problem.
})
.done(function(){ //but this gets called all the time at end no matter success or fail
      //release system resource like connection etc.
})

我试图在 cmets 中解释问题,基本上我已经完成了 self.getDetail() 调用,一旦成功,我想退出承诺队列,原因,如果 self.onResult(data) 有问题的方式之后,.fail() 也会被触发,因为它是它的依赖项。

我尝试将我的调用放入 .done() 方法中,但无论成功还是失败,done() 都会被调用。

我有一个失败的例程,它被 .fail() 函数调用,但不知道是否有成功的处理程序。

欢迎任何横向思考。

编辑 - 在 Barmar 的 cmets 之后,我们可以这样做吗? (getConnection 返回一个承诺并拒绝/解决

connections.getConnection(function(c){
    return self.getMaster(c) //async routine, returns promise, does reject/resolve
}, function(info){
     self.onFault(info) //any failures in getMaster, any error in onFault shouldn't be q business
})
.then(function(data){
     return self.getDetail() //async routine, returns promise, does reject/resolve
 }), function(info){
     self.onFault(info)} //any failures in getDetail, any error in onFault shouldn't be q business
 })
.fail(function(info){ //btw any errors, onFault causes down the road shouldn't be q problem- same for above onFault calls
    self.onFault(info) //do I need this after above fail routines for each call?
 })
.done(function(){ //ok, once everything's done, get out of promise queue
     self.onResult(self.data) //any problem onResult causes down the road, should be it's own business
 }) //release routine

//异步队列中的两个独立函数,承诺链等。这些函数中的任何错误都不应影响承诺链或将其称为失败处理程序。承诺链应该在那里完成。

onResult: function(data) {
    console.log('do something with the data');
}

onFault: function(info) {
    console.log('wonder what went wrong');
}

请为上述编辑提出建议

我的主要主要要求,onResultonFault 之后发生的任何事情都不应该是 q 图书馆业务 (fail),他们现在应该自己处理(之后)

【问题讨论】:

  • .then() 有两个参数:第一个是成功函数,第二个是失败函数。
  • 哦,我明白了,让我在我的编辑中为您的 cmets 重写一些内容
  • this question 表示 .then 相当于 jqXHR 的 .success。您确定 getConnection() 首先报告失败吗?

标签: javascript promise q


【解决方案1】:

您传递给then 的第一个函数本身就是一个成功处理程序。

一个操作的返回值,例如: doSomething().then(function() { ... }) 是一个新的承诺,您可以随时将其存储在一个变量中,然后使用多个独立的then 调用:

var promise = someOperation().then(function(x) {
  return doSomethingWith(x);
});
promise.then(function(processedX) {
  // processedX is the return value of the function used to construct `promise`
  // now for something completely different
}
promise.then(someOtherFunction);

您不需要无限期地链接,您可以通过将中间的新 Promise 存储到变量中并在其他地方使用它们(可能多次)来“脱离 Promise 链”,并创建多个独立的链。

这样,您可以将fail 处理程序附加到一个链中的一个相同的promise,而在另一个链中没有附加到它。在您的情况下,您希望将整个链存储到一个变量中调用self.onResult 的处理程序,在该变量上使用fail 处理程序,然后使用相同的变量继续执行其余代码。

【讨论】:

  • 感谢您的回答,我已经做了一些修改,请您根据我的修改进行修改
  • 我已经阅读了编辑,但我不知道他们如何使我的答案值得修改,抱歉。
  • 你的答案很好,只是你可以在编辑部分对你的答案发表评论,复制我的代码(从上面的编辑中)并更改任何评论的行以纠正我的错误
  • 基本上我的大多数项目都有两个链式异步调用,每个调用都有成功和失败处理程序,然后是最终成功调用(退出队列)
  • 这属于一个新问题。你不应该链接两个异步调用,你应该为它们创建承诺并使用Q.all。看来您从根本上对 Promise 的用法感到困惑,在这种情况下,您应该阅读 Chapter 3, Promises of YDKJS: async & performance
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 2020-07-01
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 2015-08-24
  • 1970-01-01
相关资源
最近更新 更多