Created
May 14, 2013 13:37
-
-
Save bobrik/5575923 to your computer and use it in GitHub Desktop.
does anyone know an npm module for this?
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
(function() { | |
var func, started; | |
function sync(fn) { | |
var running = false, | |
requested = []; | |
return function me(callback) { | |
if (running) { | |
requested.push(callback); | |
return; | |
} | |
running = true; | |
fn(function() { | |
callback.apply(this, arguments); | |
running = false; | |
if (requested.length) { | |
me(requested.shift()); | |
} | |
}); | |
} | |
} | |
func = sync(function(callback) { | |
setTimeout(callback, 1000); | |
}); | |
started = Date.now(); | |
func(function() { | |
console.log("after a while me #1", Date.now() - started); | |
}); | |
func(function() { | |
console.log("after a while me #2", Date.now() - started); | |
}); | |
func(function() { | |
console.log("after a while me #3", Date.now() - started); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: