Guest User

Untitled

a guest
Jan 22nd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. function main() {
  2. var d = accessAndShowEntries();
  3. d.addCallback(function() {
  4. console.log("finish");
  5. });
  6. setTimeout(function() {
  7. console.log("cancel!");
  8. d.cancel();
  9. }, 2500);
  10. }
  11.  
  12. function accessAndShowEntries() {
  13. function loop(i) {
  14. if (i >= 5) return;
  15. return accessEntry(i).addCallback(function (data) {
  16. console.log(data);
  17. return callLater(0, loop, i + 1);
  18. });
  19. }
  20. return callLater(0, loop, 0);
  21. }
  22.  
  23. // XHR で何かアクセスする代わり
  24. function accessEntry(i) {
  25. var d = new Deferred();
  26. var cb = d.callback.bind(d, "foo"+i);
  27. var timerId = setTimeout(cb, 1000);
  28. d.canceller = function () {
  29. clearTimeout(timerId);
  30. };
  31. return d;
  32. }
Add Comment
Please, Sign In to add comment