I have learn the asynchronous process.
In my understanding,
- synchronous codes => is stacked and consumed
- asynchronous codes => first, go to the webapis, wait for a stack being empty, and consumed by going through the event loop
so, I made one simple code
///class definition///
class Works {
constructor(){
this.work = []
}
async getWork(){
const res = await axios.get("....url.....");
this.work = res.data
console.log(this.work)
}
}
///run///
const check = new Works('accounting');
console.log(check);
check.getWork();
Now the problem is,
in my thought, the result would be
console.log(check) <--- {work : []}
check.getWork(); <---{work:[{...},{...},.....]}
but the result is
console.log(check) <----{work:[{...},{...},.....]}
check.getWork(); <---{work:[{...},{...},.....]}
Why....
I thought that console.log(check) is synchronous one so that the "work" array wasn't updated at this juncture.
Could anyone tell me the reason?