Advertisement
framp

Sequence of actions

Nov 11th, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //1
  2.  
  3. function scrape(data, cb){
  4.   setTimeout(function(){
  5.     console.log(data);
  6.     if (cb) cb();
  7.   }, 1000);
  8. }
  9. function compose(args, cb){
  10.   var l = args.length;
  11.   var i = 0;
  12.   if (l===0) return;
  13.   var next = function(){
  14.     if (i++>=l)
  15.       return cb();
  16.     args[i-1](next);
  17.   };
  18.   next();
  19. }
  20.  
  21. var requests = [
  22.   scrape.bind(null, 'url1'),
  23.   scrape.bind(null, 'url2'),
  24.   scrape.bind(null, 'url3')
  25. ]
  26.  
  27. compose(requests, console.log.bind(console, 'over'));
  28.  
  29. //2
  30.  
  31. function firstAction(data){
  32.   setTimeout(function(){
  33.     console.log(data[0]);
  34.     secondAction(data);
  35.   }, 1000);
  36. }
  37.  
  38. function secondAction(data){
  39.   setTimeout(function(){
  40.     console.log(data[1]);
  41.     thirdAction(data);
  42.   }, 1000);
  43. }
  44.  
  45. function thirdAction(data){
  46.   setTimeout(function(){
  47.     console.log(data[2]);
  48.   }, 1000);
  49. }
  50.  
  51. firstAction("abc");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement