سؤال

I need to use a queue to serialize my async requests. Looking around, I've found a small library by Mike Bostock here. But I am a bit confused as tp how to use it along with promise object.

So, I have tons of reqs coming from the user interface.

function addTask(d){
   AsyncOper(d)
      .then(function () {
             refresh()
      });
}

AsyncOper returns a promise object (angular js implementation - $q).

I have a q defined as

var q = queue(1);

How can I turn addTask to use q

My first attempt was as follows:

    function addTask(d){
       q.defer(request, d)
       q.awaitAll(function(error, results) { console.log("all done!"); });
    }
    function request(d, cb) {
        AsyncOper(d)
            .then(function () {
                refresh();
                cb(null, "finished "+ d);
            })
    }

But it is not really serializing the operation, since I see it trying to run more than one request. Is it possible to combine promise and queue this way or is there a better way?.

Thank you.

هل كانت مفيدة؟

المحلول

https://github.com/mbostock/queue says, about await and awaitAll:

This method should only be called once, after any tasks have been
deferred. If the await callback is set multiple times, or set before 
a task is deferred, the behavior of the queue is undefined.

At the moment, you call awaitAll after every time you defer a task, which is some definite dragon-tickling. Is there a place you can all awaitAll after queueing everything? If not, this particularly library may not be right for you.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top