What will happen if i call a sync function in the.then
part of an async await .then
? I.e:
await asyncFunc(...)
.then(res => sendMSG(res))
.catch(err => next(err));
function sendMSG(res){
xyz
}
sendMSG
is a sync function called in the async part .then
. What will happen if multiple asyncFunctions call sendMSG at once?
CodePudding user response:
What will happen if i call a sync function in the.then part of an async await .then?
Then it will be called.
at will happen if multiple asyncFunctions call sendMSG at once?
JavaScript operates on an event loop. Only one function will ever be running at a time.
An asynchronous function will be put on hold until whatever asynchronous business it is doing is done. Next time the event loop is free it will pick up the results of the asynchronous business and continue.
It will then block other asynchronous functions from picking up when their business is done until it is finished.
CodePudding user response:
Javascript is fundamentally single thread. So there's no such thing as multithreading concurrency (some code being simultaneously executed on different threads).
Async (and Promises also) is just syntactic sugar for callbacks.