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.