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);
	});
}, function(err, fullRes){
	console.log('done');
	console.log(fullRes);
});

Leave a Reply