1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| function processAndEnqueue(queue, process, value){ queue.enqueue(process(value)); }
function slowResolve(queue, resolutionProcess){ setTimeout(function(){ if(queue.peek() !== null){ resolutionProcess(queue.dequeue()); slowResolve(queue, resolutionProcess); } }, 0); }
function fakeAsyncRequest(callback, value){ setTimeout(callback(value), 10); }
function square(value){ return value * value; }
function enqueueSquares(queue){ var resolutionCallback = processAndEnqueue.bind(null, queue, square); for(let i = 0; i < 10; i++){ fakeAsyncRequest(resolutionCallback, i); } }
function logSquares(queue){ var log = console.log.bind(console); slowResolve(queue, log); }
var squareQueue = new Queue();
enqueueSquares(squareQueue); logSquares(squareQueue);
|