Utilizing the Queue Library from Async.js for downloading multiple assets with Appcelerator Titanium
// | |
// https://github.com/caolan/async | |
// | |
var async = require('async'); | |
// this function will be called for each array element | |
function process(_url, _processCallback) { | |
// download the file | |
get_file(_url, function(_resp) { | |
if (!_resp.error) { | |
Ti.API.debug('file successfully downloaded ' + _url); | |
} else { | |
Ti.API.error('Error Downloading file ' + _url); | |
Ti.API.error('Error Message ' + _resp.error); | |
} | |
_processCallback(_resp.error); | |
}, null); | |
}; | |
// create the queue | |
var myQueue = async.queue(process, 3); | |
// called when the process is completed | |
myQueue.drain = function() { | |
Ti.API.info('all items have been processed'); | |
}; | |
// START THE PROCESS | |
// processes all of the models in the collection | |
myQueue.push(files_arr, function(_err) { | |
_err && Ti.API.error("error processing queued model " + JSON.stringify(_err)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment