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);
});
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="">