Created
September 4, 2014 18:55
-
-
Save jwerle/9f577b4c329ef1cbca79 to your computer and use it in GitHub Desktop.
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
var queue = []; | |
function enqueue (work) { | |
queue.push(work); | |
} | |
function dequeue () { | |
for (var i = 0; i < queue.length; ++i) void function (work, i) { | |
work( | |
function (isDone) { | |
if (isDone) { | |
queue[i] = null; | |
} | |
} | |
); | |
}(queue[i], i); | |
queue = queue.filter(function (work ) { return null != work; }); | |
return queue.length; | |
} | |
function mySetTimeout (fn, ms) { | |
var stop = Date.now() + ms; | |
enqueue(function (callback) { | |
console.log('poll') | |
if (Date.now() >= stop) { | |
fn(); | |
callback(true); | |
} else { callback(false); } | |
}); | |
} | |
mySetTimeout(function () { | |
var foo = 'bar'; | |
var o = new Object(); | |
console.log('500ms'); | |
mySetTimeout(function () { | |
console.log('1000ms'); | |
}, 1000); | |
}, 500); | |
console.log('woo') | |
// while there are messages continue else break | |
while (dequeue()) ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment