Created
September 8, 2011 06:26
-
-
Save fujidig/1202768 to your computer and use it in GitHub Desktop.
This file contains 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 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