2-18 What about asynchronous callback?

bigword alert : asynchronous

  • more than one at a time

  • asking other elements -> asynchronous
  • inside js -> synchronous

Example : Event Queue

  • js engine looks at the queue periodically only when stack is empty
  • browser puts the event asynchronously
  • js run the event handler line by line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// long running function
function waitThreeSeconds() {
var ms = 3000 + new Date().getTime();
while (new Date() < ms){}
console.log('finished function');
}

function clickHandler() {
console.log('click event!');
}

// listen for the click event
document.addEventListener('click', clickHandler);


waitThreeSeconds();
console.log('finished execution');