まあ言及する価値があります。 jQueryが許可されている場合 - jQuery promiseインターフェイスとjQuery.Deferredは単なるものです:
// Create a Deferred and return its Promise
function asyncEvent(){
var dfd = new jQuery.Deferred();
setTimeout(function(){
dfd.resolve("hurray");
}, Math.floor(Math.random()*1500));
setTimeout(function(){
dfd.reject("sorry");
}, Math.floor(Math.random()*1500));
return dfd.promise();
}
// Attach a done and fail handler for the asyncEvent
$.when(asyncEvent()).then(
function(status){
alert(status+', things are going well');
},
function(status){
alert(status+', you fail this time');
}
);
もう1つの例、私はなるだろう
function doAjax(){
return $.get('foo.htm');
}
function doMoreAjax(){
return $.get('bar.htm');
}
$.when(doAjax(), doMoreAjax())
.then(function(){
console.log('I fire once BOTH ajax requests have completed!');
})
.fail(function(){
console.log('I fire if one or more requests failed.');
});
は '非同期classes'何ですか? – davin
@ダビン、固定質問 – Kyle