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);
		}
	});		
}
This entry was posted in JavaScript, async.js. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">