Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function asyncAddOne(x, callBack) {
  2.   setTimeout(function() {
  3.     return callBack(x + 1);
  4.   }, 200);
  5. }
  6.  
  7. function asyncDouble(x, callBack) {
  8.   setTimeout(function() {
  9.     return callBack(x * 2);
  10.   }, 200);
  11. }
  12.  
  13. function asyncTimesTen(x, callBack) {
  14.   setTimeout(function() {
  15.     return callBack(x * 10);
  16.   }, 200);
  17. }
  18.  
  19. // Create this function!
  20. function waterfall(arg, tasks, cb) {
  21.   let count = 0;
  22.   let x = arg;
  23.   if(count < tasks.length) {
  24.  
  25.     tasks[count](x, function (x) {
  26.       count++;
  27.       tasks[count] (x, function(x) {
  28.         count++;
  29.         tasks[count] (x, function(x) {
  30.           count++;
  31.           cb(x);
  32.         });
  33.       });        
  34.     });
  35.   };
  36. }
  37.  
  38. waterfall(3, [asyncAddOne, asyncDouble, asyncTimesTen], function(result) {
  39.   console.log('Test 1');
  40.   if (result !== 80) {
  41.     console.log('test failed, expected 80 but got', result);
  42.   } else {
  43.     console.log('Test 1 passed!');
  44.   }
  45. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement