Created
June 5, 2013 20:02
-
-
Save seejohnrun/5716831 to your computer and use it in GitHub Desktop.
A quick runner for doing operations (either synchronous or asynchronous), in series
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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