
Untitled
By: a guest on
Jun 20th, 2012 | syntax:
JavaScript | size: 1.08 KB | hits: 10 | expires: Never
//var util = require("util"); // check
function prepare_file(path, callback) {
setTimeout(function () {
callback(path + "_found");
// what if we change a little our requirements?
/*
if(path!='c')
callback(path + "_found");
else callback();
*/
}, 1000*Math.random());
};
function process_files(files, callback){
var cont = {}
, remains = files.length;
function process_file_intern(path){
prepare_file(path, function(path_result){
if(path_result) cont[path] = path_result;
if(--remains===0){// last file? run callback then
var ret = [];
for(var i = 0; i<files.length;++i){
var curr_file_path = cont[files[i]];
if(!curr_file_path) continue;
ret.push(curr_file_path);
}
callback(ret);
}
});
};
for(var i = 0; i<files.length;++i){
process_file_intern(files[i]);
}
}
// what if you pass other items like 'd_', 'other_file_if_found'
process_files(['a', 'b', 'c'], function (paths) {
// console.log("result:"+util.inspect(paths)); //check
// paths should equal ['a_found', 'b_found', 'c_found']
});