cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console