0

Is there a handler method, the counterpart of fail, something like success to really get out of that async queue and continue with your normal function calls.

Let me elaborate more. let's say

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

I've tried to explain the problem in comments, basically I'm done at self.getDetail() call, once it's success, I want to get out of promise queue, reason, if self.onResult(data) had a problem way afterwards, the .fail() is gonna get triggered too as it's sort of it dependency there.

I tried putting my call in the .done() method but done() gets called no matter what, success or fail.

I've a failed routine that gets called by .fail() function but don't know if there's a success handler.

Any lateral thinking welcome.

Edit - after Barmar's comments, can we do like this. (getConnection returns a promise and does reject/resolve

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

//two independent functions out of async queue, chain of promises etc. any error in these functions should not affect the chain of promises or call it's fail handler. chain of promises should have been done up there.

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

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

please suggests for the edit above

My Main Main requirement, anything happen after onResult, onFault shouldn't be q library business (fail), they are supposed to handle it on their own now (afterwards)

user2727195
  • 7,122
  • 17
  • 70
  • 118
  • `.then()` takes two arguments: the first is a success function, the second is a failure function. – Barmar Nov 08 '14 at 19:04
  • oh I see, let me rewrite something in my edits for your comments – user2727195 Nov 08 '14 at 19:06
  • [this question](http://stackoverflow.com/questions/5436327/jquery-deferreds-and-promises-then-vs-done) says that `.then` is equivalent to `.success` for jqXHR. Are you sure that `getConnection()` reports failure in the first place? – Barmar Nov 08 '14 at 19:07

1 Answers1

2

The first function you pass into a then is in itself a success handler.

The return value of an operation like: doSomething().then(function() { ... }) is a new promise, which you can always store inside a variable, and then use multiple, independent then calls on:

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);

You don't need to chain indefinitely, you can always "get out of the promise chain" by storing intermediate new promises into variables and using them somewhere else, possibly multiple times, and create several independent chains.

With that, you can attach a fail handler to one and the same promise in one chain, and have none attached to it in another. In your case, you'd want to store the entire chain up to the handler calling self.onResult in a variable, use the fail handler on that variable, and continue with the rest of the code using the same variable.

yerforkferchips
  • 1,965
  • 1
  • 19
  • 27
  • thanks for your answer, I've done some edits, can you please revise based on my Edit – user2727195 Nov 08 '14 at 19:22
  • I've read the edits, but I don't know how they make my answer worth revising, sorry. – yerforkferchips Nov 08 '14 at 19:24
  • your answer is good, it's just if you can comment in an Edit section to your answer, copy my code (from edit above) and change any commented lines to correct me if i am wrong – user2727195 Nov 08 '14 at 19:29
  • basically most of my projects have two chained async calls each with success and fail handler followed by final success call (that gets out of queue) – user2727195 Nov 08 '14 at 19:32
  • That belongs in a new question. You shouldn't chain two async calls, you should create promises for them both and use `Q.all`. It seems you're fundamentally confused about the usage of promise, in which case you should read [Chapter 3, Promises of YDKJS: async & performance](https://github.com/getify/You-Dont-Know-JS/blob/master/async%20&%20performance/ch3.md). – yerforkferchips Nov 08 '14 at 19:36
  • yes I'm relatively new to them, I'm gonna read this chapter, question, when you say you shouldn't chain two async call, meaning chain only synchronized calls??? and if there are multiple async calls with their success handlers, I use Q.all and pass array of promises? – user2727195 Nov 08 '14 at 19:54