cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="controls">
  <button id="seriesGo">Series</button>
  <button id="parallelGo">Parallel</button>
  <button id="whilstGo">Whilst</button>
  <button id="waterfallGo">Waterfall</button>
  <button id="eachGo">Each</button>
  <button id="eachSeriesGo">Each Series</button>
</div>

<section class="code">
  <h1>Code:</h1>
  <div class="code-wrap">
    <pre id="code"></pre>
  </div>
</section>

<section class="log">
  <h1>Output:</h1>
  <div class="code-wrap">
    <pre id="log"></pre>
  </div>
</section>
            
          
!
            
              body, html {
  margin: 0;
  padding: 0;
  background: #000;
}
body {
  padding: 2em;
}
.controls {
  margin-bottom: .5em;
}
h1 {
  color: #fff;
  font-family: sans-serif;
  font-weight: normal;
  text-transform: uppercase;
  letter-spacing: .2em;
  font-size: 1em;
}
.code,
.log {
  float: left;
  width: 49%;
}
.log {
    float: right;
}
.code-wrap {
  border: .1em solid #444;
  height: 25em;
  overflow: hidden;
  margin-top: 1em;
  padding: 1em;
  background: #222;
  
  pre {
    color: #fff;
    height: 100%;
    padding: 2em;
    margin: -2em;
    overflow-y: scroll;
    -webkit-overflow-scrolling: touch;
    -ms-overflow-style: none;
    font-family: "droid sans mono", consolas, "monaco", monospace, sans-serif;
  }
}
button {
  background: #2a92d8;
  border: none;
  padding: 1em;
  color: #222;
  text-transform: uppercase;
  letter-spacing: .15em;
  cursor: pointer;
  transition: color .3s ease-in-out, background-color .1s ease-in-out;
  
  &:hover {
    color: #fff;
    background: #0f6aa7;
  }
  &:focus {
    outline: none;
  }
}
            
          
!
            
              //
//  Async Functions
//

// Series
var series = function series(fncs, done) {
  var wrap = function(i) {
    if (i === fncs.length) return done();
    fncs[i](i, function() {
      wrap(++i);
    });
  };
  wrap(0);
};

// Parallel
var parallel = function parallel(fncs, done) {
  var complete = 0;
  for (var i = 0; i < fncs.length; i++) {
    fncs[i](i, function() {
      complete++;
      if (complete === fncs.length) done();
    });
  }
};

// Whilst
var whilst = function whilst(test, fnc, done) {
  var wrap = function() {
    if (!test()) return done();
    fnc(wrap);
  };
  wrap();
};

// Waterfall
var waterfall = function waterfall(fncs, done) {
  var wrap = function(i, err) {
    var args = Array.prototype.slice.call(arguments, 2);
    if (err) {
      args.unshift(err);
      return done.apply(null, args);
    }
    if (i === fncs.length) {
      args.unshift(null);
      return done.apply(null, args);
    }
    args.push(function() {
      var a = Array.prototype.slice.call(arguments, 0);
      a.unshift(++i);
      wrap.apply(null, a);
    });
    fncs[i].apply(null, args);
  };
  wrap(0);
};

// Each
var each = function each(arr, iterator, callback) {
  var complete = 0,
      hasErrored = false;
  for (var i = 0, len = arr.length; i < len; i++) {
    iterator(arr[i], function(err) {
      complete++;
      if (err) {
        hasErrored = true;
        callback(err);
      }
      if (!hasErrored && complete === len) {
        callback();
      }
    });
  }
};

// Each Series
var eachSeries = function eachSeries(arr, iterator, done) {
  var wrap = function(i) {
    if (i === arr.length) return done();
    iterator(arr[i], function(err) {
      if (err) done(err);
      wrap(++i);
    });
  };
  wrap(0);
};

// Each Limit
var eachLimit = function eachLimit(arr, limit, iterator, callback) {
  var i = 0,
	  complete = 0,
	  hasErrored = false;

  var callFnc = function(index) {
	iterator(arr[index], function(err) {
	  complete++;
	  if (err) {
		  hasErrored = true;
		  return callback(Err);
	  }
	  if (!hasErrored && complete === arr.length) {
		  return callback();
	  }
	  callFnc(i);
	  i++;
	});
  };

  while (i < limit) {
	callFnc(i);
	i++;
  }

};

//
//  Using the functions
//

// Series
var seriesGo = function() {
  var asyncFunc = function(val, done) {
    setTimeout(function() {
      log(val);
      done();
    }, (Math.floor(Math.random() * 2) + 0) * 1000);
  };

  series([
    asyncFunc,
    asyncFunc,
    asyncFunc,
    asyncFunc,
    asyncFunc
  ], function() {
    log('Done!');
  });
};

// Parallel
var parallelGo = function() {
  var asyncFunc = function(val, done) {
    setTimeout(function() {
      log(val);
      done();
    }, (Math.floor(Math.random() * 2) + 0) * 1000);
  };

  parallel([
    asyncFunc,
    asyncFunc,
    asyncFunc,
    asyncFunc,
    asyncFunc
  ], function() {
    log('Done!');
  });
};

// Whilst
var whilstGo = function() {
  var count = 0;
  whilst(function () {
    return count < 5;
  },
  function (callback) {
    count++;
    setTimeout(function() {
      log(count);
      callback();
    }, 1000);
  },
  function (err) {
    log('Done!');
  });

};

// Waterfall
var waterfallGo = function() {
  waterfall([function(callback) {
    callback(null, 'one', 'two');
  }, function(arg1, arg2, callback) {
    log(arg1 + ', ' + arg2);
    callback(null, 'three');
  }, function(arg1, callback) {
    log(arg1);
    callback(null, 'Done!');
  }], function (err, result) {
    log(result);
  });
};

// Each
var eachGo = function() {
  var asyncFunc = function(val, done) {
    setTimeout(function() {
      log(val);
      done();
    }, (Math.floor(Math.random() * 2) + 0) * 1000);
  };

  each([1, 2, 3, 4, 5, 6], asyncFunc, function() {
    log('Done!');
  });
};

// Each Series
var eachSeriesGo = function() {
  var asyncFunc = function(val, done) {
    setTimeout(function() {
      log(val);
      done();
    }, (Math.floor(Math.random() * 2) + 0) * 1000);
  };

  eachSeries([1, 2, 3, 4, 5, 6], asyncFunc, function() {
    log('Done!');
  });
};

// Each Limit
var eachLimitGo = function() {
  var asyncFunc = function(val, done) {
    setTimeout(function() {
      log(val);
      done();
    }, (Math.floor(Math.random() * 2) + 0) * 1000);
  };

  eachLimit([1, 2, 3, 4, 5, 6], 2, asyncFunc, function() {
    log('Done!');
  });
};

//
//  Setup
//

// Helpers
var codeEl = document.getElementById('code');
var logEl = document.getElementById('log');
var log = function(text) {
  logEl.innerHTML = logEl.innerHTML + text + '\n';
};
var clear = function(text) {
  logEl.innerHTML = '';
  logEl.scrollTop = 0;
};
var runFunc = function(name) {
  return function() {
    clear();
    codeEl.scrollTop = 0;
    codeEl.innerHTML = [
      '// ' + name.charAt(0).toUpperCase() + name.slice(1),
      window[name].toString(),
      '\n// Calling ' + name,
      window[name + 'Go'].toString()
    ].join('\n');
    window[name + 'Go']();
  }
};

// Bind the buttons
document.getElementById('seriesGo').onclick = runFunc('series');
document.getElementById('parallelGo').onclick = runFunc('parallel');
document.getElementById('whilstGo').onclick = runFunc('whilst');
document.getElementById('waterfallGo').onclick = runFunc('waterfall');
document.getElementById('eachGo').onclick = runFunc('each');
document.getElementById('eachSeriesGo').onclick = runFunc('eachSeries');
//document.getElementById('eachLimitGo').onclick = runFunc('eachLimit');


            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console