I've created this code which can call functions/ajax requests or pieces of code sequentially, using jQuery. The code not only can call functions/ajax requests or pieces of code sequentially but it also tracks when those calls fail or succeed and when the code fails it stops and doesn't exectue the rest of the queue.
(Bold italic are the names of the queues)
In my example i've 5 timeouts who get queued, the 2 first share a queue named first (1 and 1b) get executed at the same time and when those 2 finish and succeed(fire resolve) the next on the queue gets executed (next in queue is two which contains only 1 timeout).
After two gets executed and succeed the third gets executed. I've coded in a way that the third fires a reject which changes the state of promise to reject(fails the request) and thanks to that the last timeout doesn't get called(to simulate a failed ajax request).
I know the code is pretty messy (it's my first version). I would like some ideas on how I could structure this better or reduce the code considerably.
A problem I have with my code is that when I add a setTimeout
, I have to include the deferred.resolve()
in it. I don't see a way around this. Does anyone have an idea how to resolve this problem?
Code: http://jsfiddle.net/g55PC/
function myqueue(){
var myself=this;
var ord=[];// contains names used in promiseArr
var funcArr=[];// contains setTimeout functions
var me = $(document);
this.add = function (func, name) {
if (typeof funcArr[name] !== "object") funcArr[name] = [];
funcArr[name].push(func);
if($.inArray(name, ord)==-1){
ord.unshift(name);
}
}
this.call= function (name) {
me.queue("deferQueue", function () {
var promiseArr=[];
for(func in funcArr[name]){
promiseArr[func]=(function(){
var dfd = new jQuery.Deferred();
funcArr[name][func](dfd);
return dfd.promise();
})();
}
$.when.apply($,promiseArr).then(function () {
console.log("Success "+name);
me.dequeue("deferQueue");
}, function () {
console.log("Fail "+name);
});
});
}
this.start = function () {
while(ord.length>0) {
this.call(ord.pop());
}
me.dequeue("deferQueue");
}
};
myPlugin = new myqueue();
myPlugin.add(function (dfd) {
setTimeout(function () {
$("#result").append("<div>1</div>");
//console.log("1");
dfd.resolve();
}, 2000);
}, "first");//first = queue Name
myPlugin.add(function (dfd) {
setTimeout(function () {
$("#result").append("<div>1b</div>");
//console.log("1b");
dfd.resolve();
}, 1000);
}, "first");//first = queue Name
myPlugin.add(function (dfd) {
setTimeout(function () {
$("#result").append("<div>2</div>");
//console.log("2");
dfd.resolve();
}, 1000);
}, "second");//second = queue Name
myPlugin.add(function (dfd) {
setTimeout(function () {
$("#result").append("<div>3</div>");
//console.log("3");
dfd.reject();
}, 500);
}, "third");//third = queue Name
//forth will not be fired since third failed
myPlugin.add(function (dfd) {
setTimeout(function () {
$("#result").append("<div>4</div>");
//console.log("4");
dfd.resolve();
}, 3000);
}, "forth");//forth = queue Name
myPlugin.start();