For the below code, how to call the downloadFile() function sequentially for each of the entry in the arrUrls[] - using the npm Q promise library, so that only one file gets downloaded at a time.
var q = require("q");
var arrUrls = ['http://1.com', 'http://2.com']; //content/length of this array will actually be dynamic.
var downloadFile = function(link)
{
var deferred = q.defer();
var requesthandler = function(url, error, response, html)
{
//format response and return
deferred.resolve(response);
}
request(urloptions, requesthandler.bind(null, link));
return deferred.promise;
};
I know this can be done straight forward using npm async library's async.mapSeries(). But, async uses callback approach, and I would prefer a solution using promise if possible.