con este codigo intento obtener un array con el formato: [name: 'nombre', type: 'dir o file',...]
Código
function getFilesInFolder(realPath, callback){ fs.readdir(realPath, (err, files) => { if(err)return callback(0); if(!files.length)return callback([]); var r = []; for(var i =0;i<files.length;i++){ var f = files[i]; fs.stat(realPath + '/' + f, (err, stat) => { if(err)return r.push(0); var a = {name: f}; // Problema, la variable f cambia hasta el ultimo archivo if(stat.isFile())a.type = 'file'; else if(stat.isDirectory())a.type = 'dir'; r.push(a); if(r.length == files.length){ r.filter((e) => {return e;}) console.log('final R: ', r); callback(r); } }); } }); }
Tiene que ser asincrono. Entiendo el problema pero no se como solucionarlo.
Un saludo