Created
July 4, 2014 09:21
-
-
Save nmalkin/1c950c0a3879d9105c54 to your computer and use it in GitHub Desktop.
Barrier demonstration
This file contains hidden or 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
"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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what is timer at the bottom