I have the following code:
"use strict";
const fs = require('fs');
class Foo {
run() {
this.files = [];
fs.readdir(__dirname, (err, dir) => {
for (let filePath of dir) {
this.files.push(filePath); // dosent change this.files outside the anonymous function
}
});
console.log(this.files);// outputs []
}
}
let foo = new Foo();
foo.run();
I want it to output the list of files.
However it gives "[]" instead, indicating "this.files" did not change.
Note I am using Node and Typescript (but a JS solution will also do).