HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Javascript Queue for async functions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javascriptasyncforfunctionsqueue

Problem

Based on the answer to my previous question on Stack Overflow, I put together the following Queue class. I realize there are already libraries out there to do this. However, I wanted to actually understand the concept and implementing it yourself really drives the knowledge home (it also provides much more fun and a greater sense of accomplishment!).

But I DO want to make sure I've done this in the best way. Please see if it has any obvious issues or lacks any necessary features. A few possible improvements:

-
Allow the list of functions passed to the constructor to be separate arguemnts, rather than an array of objects, by converting the arguments object into an array.

One potential problem with this is if, in answer to #6 below, you guys feel the constructor should accept a context to be applied as this when calling the queued functions. To my mind, it seems more intuitive to have var Queue = function( arrayOfFuncs, context ) { than this:

var Queue = function() {
    var list = Array.prototype.slice.call( arguments ),
        context = this,
        self = this;
    if (typeof list[0] !== 'function') {
        context = list.shift(); // Context would have to be the first argument passed in
    }
    // ...
};


-
Similarly deal with returnValue as an array, so that a variable number of arguments could be passed to the callback.

  • Adding onComplete or onError handlers, allowing easier access to the final return value.



  • Add more helper functions like 'delay'. (For instance, a 'timer' method which handles an array, sending the items in the array to a specified processing function in batches. I.e. a fancy way to perform common setInterval patterns.)



  • Perhaps methods to add or delete items from the queue?



  • What should be passed as this to each function in the queue? Right now I am passing the queue itself, to allow the functions access to this.paused if they want to pause an interval (i.e. keep returning from that interval until no lon

Solution

From a once over,

  • I would check for if (self.paused) { at the very beginning, otherwise you might keep pushing returnValue into the args



  • I would not assign an anonymous function herre: this.next = function(returnValue) { I would go for this.next = function next(returnValue) { this will make life easier when looking at stacktraces



  • It does not make sense to maintain both self.running and self.paused since self.running will always be !self.paused



  • I would write if (!list[i].args) list[i].args = []; as


list[i].args = list[i].args || []

  • JsHint has nothing to report



On the whole though, I am not sure I would use this library. I think I would go hunt for an existing queue library.

Context

StackExchange Code Review Q#30298, answer score: 2

Revisions (0)

No revisions yet.