Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are many ways to write this kind of thing. </p> <p>A flexible approach is separate "actions" from "sequence", allowing :</p> <ul> <li>functions a, b, c to initiate an asynchronous (ajax) action, with no knowledge of how they are to be sequenced</li> <li>a, b, c to be reusable, as part of one or more sequences or individually, as required.</li> </ul> <p>Here's a way to code this approach, using <code>.then()</code> exclusively for the chaining logic :</p> <pre><code>function a() { return $.post(...).then(function(result) { if(result) return result;//continue on "success" path. else return $.Deferred().reject('a').promise();//convert success to failure. }, function() { return 'a';//continue on failure path. }); } function b() { return $.post(...).then(function(result) { if(result) return result;//continue on "success" path. else return $.Deferred().reject('b').promise();//convert success to failure. }, function() { return 'b';//continue on failure path. }); } function c() { return $.post(...).then(function(result) { if(result) return result;//continue on "success" path. else return $.Deferred().reject('c').promise();//convert success to failure. }, function() { return 'c';//continue on failure path. }); } a().then(b).then(c).then(function() { console.log("successful"); }, function(id) { console.log("failed: " + id); }); </code></pre> <p>Alternatively, if you want to have a single asynchronous function, <code>a</code>, called from within a loop then the code could be something like this :</p> <pre><code>function a(obj) { return $.post(...).then(function(result) { if(result) return result;//continue on "success" path. else return $.Deferred().reject(obj.id).promise();//convert success to failure. }, function() { return obj.id;//continue on failure path. }); } var data = [{id:'A', param1:1235, param2:3214}, {id:'B', param1:5432, param2:9876}]; //Note how IDs are included so these data objects can be identified later in failure cases. var dfrd = $.Deferred();//starter Deferred for later resolution. var p = dfrd.promise();//A promise derived from the starter Deferred, forming the basis of a .then() chain. //Build a .then() chain by assignment $.each(data, function(i, obj) { p = p.then( function() { return a(obj); });//By not including a fail handler here, failures will pass straight through to be handled by the terminal .then()'s fail handler. }); //Chain a terminal .then(), with success and fail handlers. p.then(function() { console.log("successful"); }, function(id) { console.log("failed: " + id); }); dfrd.resolve();//Resolve the starter Deferred to get things started. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload