I'm trying to push the content of my folder inside an array named icon. However, the array is filled only inside the scope of the readdir but after that, it's empty and I don't know why.
const Discord = require('discord.js');
const client = new Discord.Client();
const path = require('path');
const fs = require('fs');
var icon = [];
function fillArray(arr) {
const dirPath = path.join(__dirname, 'media');
fs.readdir(dirPath, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(function (file) {
arr.push(file);
});
console.log('value ==> ' + arr); // array is fill here
});
console.log('empty here ==> ' + arr); // array is empty
return arr;
}
fillArray(icon);