Hi Moshe,
Have you tried using the Deferred / Promise approach, as described in the post you referenced?
With all standard actions that are asynchronous, such as “Save Models” and “Requery Models”, the Action Framework does exactly this — it waits until the asynchronous Save / Requery actions are finished before moving on to further Actions, if everything succeeds, or to On-Error Actions, if the Save/Requery has a problem.
But if you are using Snippets, it’s impossible for Skuid to “automatically” perform this sort of behavior, because Skuid doesn’t know what kinds of asynchronous processing you are doing inside your function — you as the Snippet writer are the one who has to decide at which points in your logic it is “okay” for the Action framework to proceed, and whether to “succeed”, or to “fail” and run On-Error Actions instead. All that Skuid can provide are the tools for telling Skuid to “move on” or to “fail” — which is exactly what the approach in the article above is providing. Your Snippet returns a Deferred Promise, and then you call either resolve() or reject() within the appropriate Callbacks in your code. If you call resolve(), Skuid knows to proceed with further actions, or if you call reject(), Skuid knows to run On-Error Actions.
OK perfect Zach, I was unsure of how exactly the whole Deffered/Promise thing worked (jQuery beginner). I also thought that it might only work with Irvins situation with a remote action. So basically if I’m writing a snippet that runs in middle of an action sequence, I can always do this:
var dfd = $.Deferred();<br>//do cool logic here<br>// assuming I have no on-error actions to attach a Callback to...<br>dfd.resolve(); <br>return dfd.promise();
Will this work?
Yes — you can apply the Deferred/Promise approach to any asynchronous scenario. Here’s some pseudo-code for the idea:
var dfd = $.Deferred();<br>someAsynchronousFunction({<br> onSuccess: function(result) {<br> // Run some logic to determine whether everything's cool<br> if (result.isGoodToGo) {<br> // Tell Skuid that we're okay to move on to the next action!<br> dfd.resolve();<br> } else {<br> // Tell Skuid to run on-error actions<br> dfd.reject();<br> }<br> },<br> onError: function(result){<br> // Tell Skuid to run on-error actions<br> dfd.reject();<br> }<br>});<br>return dfd.promise();