My question is regarding a very basic concept of JS. To understand the beauty of Promise
and Async-Await
I created a normal code first which executes line by line.
I've been taught that in traditional code, if one line is busy with some process for few seconds then remaining lines of code will have to wait. My understanding is this:
line of code 1 // completed
line of code 2 // completed
server interaction // waiting for response from server
line of code 3 // waiting
line of code 4 // waiting
line of code 5 // waiting
To mimic this server delay I used setTimeout(3000)
but still I can see rest of the lines are being executed normally. Here's my code:
ngOnInt() {
console.log("hello");
console.log("how");
setTimeout(()=>{
console.log("i was waiting");
}, 3000);
console.log("are");
console.log("you");
}
Output:
hello
how
are
you
i was waiting
I was expecting that "hello", "how" will get printed first, then after 3 seconds "are", "you" will be printed. Where I am making mistake. Please correct me.