Skip to content

Instantly share code, notes, and snippets.

@nmalkin
Created July 4, 2014 09:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nmalkin/1c950c0a3879d9105c54 to your computer and use it in GitHub Desktop.
Save nmalkin/1c950c0a3879d9105c54 to your computer and use it in GitHub Desktop.
Barrier demonstration
"use strict";
function longRunningFunction1(onComplete) {
setTimeout(onComplete, 3000);
}
function longRunningFunction2(onComplete) {
setTimeout(onComplete, 2000);
}
function executeSequentially() {
longRunningFunction1(function() {
longRunningFunction2(function() {
console.log("The functions running sequentially have finished.");
});
});
}
function makeBarrier(onComplete) {
var firstFinished = false;
var barrier = function() { // called by each function when it finishes
if(! firstFinished) { // first (by time) function finished
firstFinished = true;
} else { // second function finished
onComplete();
}
};
return barrier;
}
function executeSimultaneously() {
var barrier = makeBarrier(function() {
console.log("The functions running simultaneously have finished.");
});
longRunningFunction1(barrier);
longRunningFunction2(barrier);
}
executeSequentially();
executeSimultaneously();
// timer
function timer(i) {
console.log(i);
if(i < 5) setTimeout(function() { timer(i+1); }, 1000);
}
timer(1);
@jkwok91
Copy link

jkwok91 commented Jul 4, 2014

what is timer at the bottom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment