I just started to learn Node.js and I need help: At the following code that runs on Node why it is stuck in a loop while it supposedly an asynchronous method? and also not waiting the interval.
class FuelBurner{
constructor(){
console.log("construction set");
this.fuel=0;
this.rate=2.0;
this.polluted=0.0;
}
run(){
while (true){
console.log("i run");
setTimeout( function (){this.polluted+=this.rate},1000);
this.fuel--;
}
}
get_pollution(){
return this.polluted;
}
}
console.log("Hello");
machine = new FuelBurner();
machine.run();
console.log("Why it never gets here?");