Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Last active August 25, 2017 03:10
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronksaunders/6245454 to your computer and use it in GitHub Desktop.
Save aaronksaunders/6245454 to your computer and use it in GitHub Desktop.
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