Skip to content

Instantly share code, notes, and snippets.

@seejohnrun
Created June 5, 2013 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seejohnrun/5716831 to your computer and use it in GitHub Desktop.
Save seejohnrun/5716831 to your computer and use it in GitHub Desktop.
A quick runner for doing operations (either synchronous or asynchronous), in series
// A quick runner for doing operations (either synchronous or asynchronous),
// in series
var simpleSequence = function (sequenceCallback) {
// Collect the operations
var operations = [];
sequenceCallback(function (operation) {
operations.push(operation);
});
// Do the operations
(function perform() {
var op = operations.shift();
if (!op) { return; }
if (op.length === 0) {
op();
perform();
} else {
op(function () {
perform();
});
}
})();
};
// How it works
simpleSequence(function (operation) {
// Wait 200 ms
operation(function (done) {
setTimeout(function () {
done();
}, 200);
});
// Say hello
operation(function () {
console.log('hello');
});
// Wait again
operation(function (done) {
setTimeout(function () {
done();
}, 200);
});
// And then say goodbye
operation(function () {
console.log('goodbye');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment