why is objlist being resolved before loop has completed. Since the for loop is sync, I expected resolve to be run after my for loop has completed, but list with empty object is being resolved.
function myFunction(path,files){
return new Promise(function(resolve,reject){
let objlist=[];
files.forEach(function(file,index){
console.log(file)
objlist[index]={};
fs.stat(path + '\\' + file, function(err,stats){
if(err){
console.log(err.code)
console.log(err)
throw err;
}
if(stats.isDirectory()){
objlist[index].file = 'folder'
}
else if(stats.isFile()){
objlist[index].file = 'file'
}
objlist[index].name = file
console.log(objlist[index].name) //gives correct output
objlist[index].size = stats.size
});
})
console.log(objlist); //gives list of empty objects
resolve(objlist);
});
}