DEV Community

loading...

Asynchronous Javascript

varuntumbe profile image Varun Hegde ・1 min read

I have some trivial doubts regarding Asynchronous functions.

1) When await keyword is encountered in async function, what does js exactly do ?
does it skip the whole function and goes to next next instruction outside the function or will it try to execute rest of the code in async ( i personally think the first one as whatever inside a async func is supposed to be executed after the asynchronous instruction)

2) Javascript has single thread and event based model. so if it encounters asynchronous code, i have read that it works in the background executing other instruction. what does executing in the background mean (if only it has single thread)?

3) If in a code every instruction is depends on result of previous async function ,doesn’t executing in both synchronously and asynchronously becomes same ?

Kindly answer these question whenever you have time.

Thanks in advance.

Discussion (1)

pic
Editor guide
Collapse
dcsan profile image
dc

At first I thought this is an odd post for dev.to as you do not add anything but just ask questions. But then I think these are very interesting questions, so I hope you will follow-up with some answers later!

perhaps you can make some code with Typescript compiler. My understanding is that some promises code is handled internally by JS runtime which makes it hard to trace, but typescript tries to reduce it to a dispatcher loop.

In my case step-through debugging with typescript async/await blocks I often see a dispatcher loop where a list of promises is handled until exhausted.

I think what is happening is internally any 'await' callback gets added to an internal stack. But it is unclear how the stack list of internal event queue is pulled and invoked by the end of external events such as a web request or IO without blocking, unless JS runtime engine internally did have some type of threading.

Let us know what you find out!