0

I am having a hard time understanding how the pool in NodeJS works. How does async function work all at a time?

Does the event loop allocate threads from the thread pool for every async Function? If yes, please look at the code below

const saySomething = async(arg)=>{
    try{
        for(i=0;i<=10000;i++){
            console.log(arg)
        }
    }
    catch{
    }
}



saySomething("Muthu")
    .then(()=>{})
    .catch(()=>{})
saySomething("Raj")
    .then(()=>{})
    .catch(()=>{})
saySomething("Candy")
    .then(()=>{})
    .catch(()=>{})
saySomething("Python")
    .then(()=>{})
    .catch(()=>{})

While running, I am seeing like it is being executed only by one thread; even though 4 threads are running concurrently by time-sharing and context-switching in OS. If not, is my assumption wrong? Please, help me out...

hfontanez
  • 5,774
  • 2
  • 25
  • 37
MUTHURAJ D
  • 13
  • 3
  • "I am seeing like it is being executed only by one thread;" — Yes. – Quentin Apr 03 '21 at 20:57
  • "even though 4 threads are running concurrently by time-sharing and context-switching in OS" — No, there is only one thread. – Quentin Apr 03 '21 at 20:57

0 Answers0