Category Archives: async.js

async.js – download multiple files from multiple urls to first success

var async = require(’async’);   var files = [’file1′,’file2′,’file3′]; var urls = [’url1′,’url2′,’url3′];   var down = function(url, file, callback) { setTimeout(function(){ var r = Math.random() >= 0.5; console.log(’— end ‘+file+’, ‘+url+’, ‘+r); callback(r); }, 1000); };   async.concat(files, function(file, callback){ async.someSeries(urls, function(url, callback1){ down(url, file, function(isOk){ callback1(null, isOk); }); }, function(err, r){ callback(err, r); [...]

Also posted in JavaScript | Leave a comment

async.js: parallel get

/** * jednoczesne wywolanie GET’ow na adresy * zwracana jest tablica odpowiedzi w callbacku * jako drugi parametr lub error jako pierwszy */ var parallelGets = function(urls, callback) { var calls = urls.map(function(url){ return function(callback){ $.get(url, function(data){ callback(null, data); }).fail(function(jqXHR){ callback(jqXHR); }); }; }); async.parallel(calls, function(err, res){ if(err) { callback(err); } else { callback(null, res); [...]

Also posted in JavaScript | Leave a comment