The story of a mother, elder sister and little brother . (event loop)

The story of a mother, elder sister and little brother . (event loop)

Β·

2 min read

Here,

  • Sumita(Mother)- Call stack πŸ‘©
  • Pooja(Elder sister) - Web APIs πŸ‘§

  • Rahul(little Brother) - Task queue. πŸ‘¦

Sumita does only one thing at a time, for example, she wakes up cleans 🧹 up the home, then makes
breakfast πŸ₯— and does all the house chores 🏑 one by one as it comes.

Call stack does one task at a time.

But One day the washing machine 🧺 got defective ⚠ so she left the washing and jump on the next task but tells pooja to please fix the washing machine by calling the mechanicπŸ‘¨β€πŸ”§.

Here the washing task is the async function which can not be handled so right now it is delegated to the web APIs (pooja).

Pooja calls the mechanic and fixes πŸ‘ the washing machine but as her mother was busy doing other work of the house and she has to leave for his classes so she tells her little brother Rahul to inform his mother that the washing machine is fixed she can now resume washing. also given him note that she is done with her regular tasks then only informs her.

web API(Pooja) does the async task and passed to the task queue(Rahul) and the task queue waits for the call stack(Sumita) to get empty then pushes the task to the call stack one by one

console.log("cleaning done")
console.log("breakfast done")
console.log("defect in washing machine")
 // mom doing all the work one at time 
setTimeout( () => { 
console.log("washing machine fixed")
}, 5000);
// pooja did the work and rahul informs his mom after she finishes her other task
console.log("continues other task");
// after this rahul informs his mom that the washing machine is fixed

Thank you for reading folks β™₯ πŸ˜‡

Β