I have a global array called employees
that gets filled with data when I call initialize();
but when I then try to print the array in getAllEmployees()
the employee's array is empty and I don't know why. The data gets stored properly in the intitialize();
function because when I print the employees
array in the initialize();
function, it's filled with data, so I'm puzzled because employees are in the global namespace
var employees = [];
var departments = [];
var error = 0;
var fs = require("fs");
function initialize(){
fs.readFile("./data/employees.json", 'utf8', function(err, data){
if(err){
error = 1;
}
employees = JSON.parse(data);
});
fs.readFile("./data/department.json", 'utf8', function(err, data){
if(err){
error = 1;
}
departments = JSON.parse(data);
});
}
function check() {
return new Promise(function(resolve,reject){
if (error === 0){
resolve("Success");
}
else if(error === 1){
reject("unable to read file");
}
})
};
function getAllEmployees(){
check().then(function(x){
console.log(x);
console.log(employees);
}).catch(function(x){
console.log("No results returned");
});
}
initialize();
getAllEmployees();