Created
October 28, 2012 11:05
-
-
Save raimon49/3968334 to your computer and use it in GitHub Desktop.
WEB+DB PRESS Vol.71『プログラマのためのjQuery再入門』Deferredオブジェクト周りの写経
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cache = {}; | |
function getFromCacheOrAPI(url) { | |
if (!cache[url]) { | |
cache[url] = $.get(url); | |
} | |
return cache[url]; | |
} | |
getFromCacheOrAPI('/api/foo').done(function(data) { | |
console.log(data.msg); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.get('/api/foo').then(function(data1) { | |
return $.get('/api/bar', {id: data1.id}); | |
}).done(function(data2) { | |
$('#target').text(data2.msg); | |
}).fail(function() { | |
console.error('Error!'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var d = $.Deferred(); | |
console.log(d.state()); // pending | |
d.resolve(); | |
console.log(d.state()); // resolved | |
d.reject(); | |
console.log(d.state()); // resolved |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getDeferred() { | |
return $.Deferred().done(function(arg) { | |
console.log('done1: ' + arg); | |
}).fail(function(arg) { | |
console.log('fail: ' + arg); | |
}).always(function(arg) { | |
console.log('always: ' + arg); | |
}).done(function(arg) { | |
console.log('done2: ' + arg); | |
}); | |
} | |
// done1: Resolve! | |
// always: Resolve! | |
// done2: Resolve! | |
var resolved = getDeferred().resolve('Resolve!'); | |
// fail: Reject! | |
// always: Reject! | |
getDeferred().reject('Reject!'); | |
// done3: Resolve! | |
resolved.done(function(arg) { | |
console.log('done3: ' + arg); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function wait(sec, opt_arg) { | |
var d = $Deferred(); | |
setTimeout(function() { | |
d.resolve(opt_arg); | |
}, sec * 1000); | |
return d.promise(); | |
} | |
var promise = wait(3).done(function() { | |
console.log('3秒後に実行される'); | |
}); | |
promise.resolve('Promiseオブジェクトからはresolveメソッドが無いためエラー'); | |
$.Deferred().resolve('Resolve!').then(function(arg) { | |
console.log(arg); // Resolve! | |
return wait(1, 'Wait!').done(function(arg) { | |
console.log('First: ' + arg); // First: Wait! | |
}); | |
}).done(function(arg) { | |
console.log('Second: ' + arg); // Second: Wait! | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment