jQuery Deferred and Promise for sequential execution of synchronous and asynchronous functions

后端 未结 3 699
名媛妹妹
名媛妹妹 2020-12-14 21:19

If I want to have synchronous and asynchronous functions execute in a particular order I could use jQuery promise but it doesn\'t seem to work the way I\'d expect it to work

相关标签:
3条回答
  • 2020-12-14 21:48

    jQuery < 1.8 is fine WRT chaining, you just use .pipe instead of .then. 1.8 simply changed .then to be .pipe.

    0 讨论(0)
  • 2020-12-14 21:55

    Sidenote: When you use it without the array, you don't have to start with a promise. $.when({}).then(a).then(b) will do the trick just fine. You only need to make sure you don't put a inside the when.

    0 讨论(0)
  • 2020-12-14 22:12

    For jQuery prior to 1.8, this is a problem, but for new versions of jQuery, this is not a problem anymore:

    function test(){
      var d = jQuery.Deferred(), 
      p=d.promise();
      //You can chain jQuery promises using .then
      p.then(a).then(b).then(c);
      d.resolve();
    }
    test();
    

    DEMO

    Below is the demo of jQuery 1.7.2

    DEMO

    0 讨论(0)
提交回复
热议问题