control flow in javascript using async.js

For those who are new to node.js, Async.js library provides us with some cool functions for the control flow and for performing some functional operations like map,reduce,filter etc. In this post we will dig into the async.series function.

async.series is useful if we need to execute few functions one after the other.   async.series takes an array of functions and an optional callback function which will be called once all functions are executed.Each function will be passed a callback function and the callback needs to be called upon completion. Below is the example from the documentation.

function one(callback) {callback();}
function two(callback) {callback();}

async.series([one,two],function(err, results){
// results is now equal to ['one', 'two']
});

Below is the basic implementation of async.series . It is simple function which takes an array and the optional callback as arguments.

function asyncSeries (fnArray,finalCallback) {
     if (fnArray.length) {
         var fn = fnArray.shift();
         var callback = function (err) {
          if(err)
            finalCallback(err);
          else
           asyncSeries(fnArray,finalCallback);
         };
        fn(callback);
    }
    else {
      finalCallback();
    }
}

Flex Focus issue

I came across an interesting bug which is reproducible only in IE6 and IE7.  I have a web page which has a SWF file embeded.  There is a IFRAME  in that web page and that IFRAME contains a HTML input field. There is also a Flex Text Field in the SWF file.  Now the issue is the flex text field is not gaining focus after clicking on the HTML input field. There is a bug which was filed with adobe but closed without any resolution.

The SWF file and the HTML file are in two layers in the browser and focus is not getting passed to the SWF file from HTML in a IFRAME. So as a work around i needed to set the focus using javascript.

//Flex code
 private function gainCursor() : void
 {
      var textID:* = focusManager.getFocus() as TextInput;
      if (textID is TextInput)
         {
            ExternalInterface.call("deFocus");
            focusManager.setFocus(IFocusManagerComponent(textID));
         }
 }

// javascript function
function deFocus()
 {
      window.focus();
      window.document["YourSWFFilenamehere"].focus();
 }