After observing a delay in referencing after using [].map with try/catch block inside, I am curious to know the reason behind this behavior and how to prevent it.
My test scenario was as follows:
-file_picked is responsible for handling the change event of an input element with type=file
file_picked: function(e){
var flist = e.target.files, //all picked files
parsed = [], //all read successfully
errors = []; //all errored
//parsing files
_.map(flist, function(file){
var reader = new FileReader();
//setting up callbacks
reader.onload = function(e){ //@reading done
try{
var file_cont = e.target.result,
parser = new(less.Parser)({
filename: file.name
});
//running the file through the less parser
parser.parse(file_cont, function (err, tree) { //parser done
var o = {};
if (err) { //@some parser error occured
err('less parser error',err);
o[file.name] = err;
errors.push(o);
}else{ //@parsed successfully by the less parser
o[file.name] = tree.toCSS();
parsed.push(o);
}
});
}catch(e){
err('reader onload exception',arguments);
var o = {}; o[file.name] = i18n('Parsing failed');
errors.push(o);
}
};
reader.onerror = function(e){ //@reading error
err('reader onerror',arguments);
var o = {}; o[file.name] = i18n('Reading failed with error: ')+e.target.error.code;
errors.push(o);
};
//start reading
reader.readAsText( file );
});
//reading completed
console.log(parsed)
for( var i = 0; i < parsed.length; i++ ) {
console.log(parsed[i],errors);
}
}
The parsed array is visible in the console! However, it never goes through iteration, why?
Thank you in advance
PS.:_.map refers to underscore.js method, err, lg are just wrappers for console.xxx.