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

元气小坏坏 提交于 2019-11-28 21:30:06

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!