0

Implementing a queue manager for Ajax requests. I've decided on this approach instead of using promises because I will have access to the status of what is queued by inspecting private variable queue. Also, the code needed is a bit smaller.

This is a very specific case in which all ajax requests are fired in parallel but I want to guarantee the ordering on their response.

  • Use a completion promise ;-) No, seriously, I currently can't see how you are going to use this manager - and what `complete` is supposed to do. Where/how do you plug in the ajax function, and where the callback function(s) that expect the ajax results? – Bergi Apr 01 '13 at 15:25

2 Answers2

1

I think I now got what you want. Have a look at this:

function AsyncQueue() {
    var results = {},
        queue = [];
    this.add = [].push.bind(queue); // add one 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) {
    // do whatever you need to do here
    console.log(res);
};
manager.add(1, 2);
manager.complete(2, "second"); // logs nothing yet
manager.complete(1, "first"); // logs "first", then "second"
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I ended up taking a Directed Acyclic Graph, and determined what this looks like serially by just looking at it b.c. it was simple. Finally I used what I call a collapsible queue to implement. If you are interested in collapsible queue let me know and I'll put it o git hub. –  Apr 16 '13 at 22:12
  • this question is too localized so I'm voting to close it. –  Apr 16 '13 at 22:13
  • More elaborate: [How to implement a pseudo blocking async queue in JS](https://stackoverflow.com/a/47157577/1048572) – Bergi Jan 19 '21 at 22:37
0

Use Case:

When sending an ajax call do:

add(token)

When receiving an ajax in the callback do:

complete(token, text);

manageQueue

// queue snapshot
// [0] token1 - waiting - null
// [1] token2 - blocked - text
// token contains info/meta about the response_text
// item 1 is blocked waiting on item 0 to complete

var Queue = (function (){
        var queue = [],
            publik = {},
            len = 0;
        publik.add = function(token) {
            var temp = {};
                temp.token = token;
                temp.waiting = true;
                temp.text = null;
            len = queue.push(temp);
        };
        publik.complete = function(token, text) {
            var kindex,
                length;

            // should never come here
            // complete is always called after add
            if(len === 0) {
                return;
            }

            // simplest case, nothing is blocked
            if(len === 1) {
                return true;
            }

            // queued items waiting
            if(len > 1) {
                return true;
            }

            for(kindex = 0, length = queue.length; kindex < length; kindex++) {
                if(queue[kindex].waiting === true) {
                    // resolve the ones waiting.
                }
            }

            // queue the text

            // resolve previous

            // 
        };
        return pulbik;
    }());
  • Those `resolve the ones waiting; queue the text; resolve previous` comments seem a little incomplete… – Bergi Apr 01 '13 at 16:14
  • I think instead of posting other answers you should update the previous, incomplete ones. – Bergi Apr 03 '13 at 09:36