javascript - ADT - Collapsbile queue -
implementing queue manager ajax requests. i've decided on approach instead of using promises because have access status of queued inspecting private variable queue. also, code needed bit smaller.
this specific case in ajax requests fired in parallel want guarantee ordering on response.
i think got want. have @ this:
function asyncqueue() { var results = {}, queue = []; this.add = [].push.bind(queue); // add 1 token this.complete = function(token) { results[token] = arguments; while (queue[0] in results) { var t = queue.shift(); this.resolve.apply(null, results[t]); delete results[t]; } }; } asyncqueue.prototype.resolve = function() { console.log.apply(console, ["no resolver for"].concat([].slice.call(arguments))); };
usage:
var manager = new asyncqueue(); manager.resolve = function(token, res) { // whatever need here console.log(res); }; manager.add(1, 2); manager.complete(2, "second"); // logs nothing yet manager.complete(1, "first"); // logs "first", "second"
Comments
Post a Comment