Skip to content

Instantly share code, notes, and snippets.

@fujidig
Created September 8, 2011 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fujidig/1202768 to your computer and use it in GitHub Desktop.
Save fujidig/1202768 to your computer and use it in GitHub Desktop.
function main() {
var d = accessAndShowEntries();
d.addCallback(function() {
console.log("finish");
});
setTimeout(function() {
console.log("cancel!");
d.cancel();
}, 2500);
}
function accessAndShowEntries() {
function loop(i) {
if (i >= 5) return;
return accessEntry(i).addCallback(function (data) {
console.log(data);
return callLater(0, loop, i + 1);
});
}
return callLater(0, loop, 0);
}
// XHR で何かアクセスする代わり
function accessEntry(i) {
var d = new Deferred();
var cb = d.callback.bind(d, "foo"+i);
var timerId = setTimeout(cb, 1000);
d.canceller = function () {
clearTimeout(timerId);
};
return d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment