0

Hey i was wondering how i would go about dynamically call a function like the following:

async function xxx(number) { 
  if(number == 10) return
  xxx(number + 1);
}

My goal is instead of having it call 1 at a time i want it to call 3/4 and run side by side(if 1 thread has checked number 2 others wont check), the number of threads depending on the needed outcome. This is a basic version but good example of what i need done

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
KazzyDev
  • 9
  • 1
  • https://stackoverflow.com/questions/18613023/how-to-create-threads-in-nodejs – Andy Oct 24 '21 at 08:10
  • you cannot! nodejs is single threaded. stricttly single threaded when it comes to non-asyn functions like the one you have posted which is synchronous and cpu intensive. However you can spawn child processes and use message passing for communication but remember its muli process – Nidhin David Oct 24 '21 at 08:25

1 Answers1

0

You can use Web worker here to perform a task from another thread. Web worker gives that ability to run a task in background without affecting the main thread, Please check this link for more info using web worker

I will also recommend you to check the difference between Service worker and Web worker so that you can analyse what suits the best for the use case, please check this link

shankar upadhyay
  • 883
  • 12
  • 18