Jquery Promis
Success =========>>> done
Fail =========>>> fail
Features:
1.support the "Chain" Call Back
$.ajax("test.html")
.done(function(){ alert("S1!");} )
.fail(function(){ alert("F1"); } )
.done(function(){ alert("S2");} );
-------If success, alert("S1") then alert("S2")
1.support mutiple call point to one Call Back
$.when($.ajax("test1.html"), $.ajax("test2.html"))
.done(function(){ alert("哈哈,成功了!"); })
.fail(function(){ alert("出错啦!"); });
-------Once two ajax call both are success ,then call done(),either call fail()
3.Three status for the deferred object
要说清楚这个问题,就要引入一个新概念"执行状态"。jQuery规定,deferred对象有三种执行状态----未完成,已完成和已失败。如果执行状态是"已完成"(resolved),deferred对象立刻调用done()方法指定的回调函数;如果执行状态是"已失败",调用fail()方法指定的回调函数;如果执行状态是"未完成",则继续等待,或者调用progress()方法指定的回调函数(jQuery1.7版本添加)。
4.deferred 'then','always'
to glue 'Done' and 'Fail',use 'then' function
$.when($.ajax( "/main.php" ))
.then(successFunc, failureFunc ); OR .then(successFunc);
to always execute function,use 'always'
$.ajax( "test.html" )
.always( function() { alert("已执行!");} );
http://www.infoq.com/cn/news/2011/09/js-promise
http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html