javascript - Handling branches with Promises -
I have a problem with the promises of jQuery 1.9.1, where I need conditional logic, which is another deferred And I'm not sure how to handle it. It was my best effort, but as the comment indicates below, when I hit the other branch, I still make a second hit. Then () function, where I hope I can go back to the user. How to handle this kind of scenario?
storage.provision (c) .then (function (rc) {if (rc === 0) {storage.write (c);} Else {return options.onSuccess (rc) ; // How the user came back for a callback / promise, but it takes me down then.}}} //storage.write also gives a promise, let me do another. So // like this? .then (function (rc) {// I was hoping that it will catch the storage. Written () case, and it does, but it also catches the option in the other option / again. OnSuccess (Rc);}) .file (function (e) {// handle error .reject ()}); It is easy to see that options.onSuccess (rc) is
; is executed in another .then () unconditionally but never before. - If the
RC
Code> RC! == 0
- or immediately
-
due to completion . . . .then () Actually this is easy because it naturally done callback to the value of any new promise. storage.provision (c) .then (function (rc) {if (rc === 0) {var dfrd = $ .defird (); storage.write (c) .done (Function () {dfrd.resolve (rc);}) .Fail (dfrd.fail); return dfrd.promise ();} else {return rc; // Pass RC to another.) Then ()}}) . Then (function (rc) {options.onSuccess (rc);}). Fail (function (e) {// error using error .reject ()}); I'm sure other approaches exist but this is the closest I can think of on my basic concept.
It would be nice that you do not have to make new deferred when RC === 0 is the most realistic approach to go to RC , To avoid the need to modify storage.write () , behave this way.
Comments
Post a Comment