I'm trying to push an instance of a class to an array within a forEach loop, but nothing gets pushed to the array for some reason.
The loop is inside a class method, and right up until the console.log
, everything looks fine (The Device
code is tested and working , the Device.build()
method populates some member variables inside the Device)
class DeviceManager {
constructor() {
this.deviceList = [];
}
async buildDevices() {
const deviceNames = await this.getDeviceNames();
deviceNames.forEach(async name => {
const device = new Device(name);
await device.build();
console.log(device); // This outputs the device as expected!
this.deviceList.push(device); // However, the device doesn't end up in this array?
});
}
...
...
}
I create an instance of DeviceManager, then call await DeviceManager.buildDevices()
.
After this, I expect deviceManager.deviceList
to be full of the devices, however it's empty, all I get back is []
What's going on here? Does anyone have an explanation?