JSFiddle - React, Tailwind, and code Playground

by toddanglin

JavaScript

Queue = function () {
  this.promise = jQuery(this).promise(); // Stub promise
};
 
// Magic with arguments is needed to pass arguments to the called function
Queue.prototype.append = function () {
  var args = arguments;
 
  var fn = args[0];
  if (!fn || !jQuery.isFunction(fn)) {
    throw new TypeError('1st parameter should be a function');
  }
 
  var self = this;
  args = Array.prototype.slice.call(args, 1);
  return this.promise = this.promise.pipe(function () {
        return fn.apply(this, args);
  });
};

//TEST
//Almost works..BUT...async functions don't happen in parallel. Everything is 
//happening synchronously.
var q = new Queue();

for(var i = 0; i < 3; i++){
    q.append(function(){
        var idx = arguments[0];
        return $.ajax({url:'/echo/json/',data:{delay:1}})
            .success(function(){ 
                alert("Done "+ idx);
            });
    }, i);
}

q.promise.done(function(){
   alert("Done with everything"); 
});