1
const fetchPromise = fetch('example.com') // Takes 1 second to resolve
...
... // Do 3 seconds of work
...
const response = await fetchPromise // What happens with the promise between seconds 2-3?

What happens with the promise response before you resolve the promise?

In the example above, what happens with the fetch response in those 2 seconds where your program has items in its call stack, but you don't settle the promise? Does Node have some kind of cache for completed but unresolved promises?

BryceLarkin
  • 476
  • 1
  • 6
  • 16
  • Actually, your program has *no* items on the call stack while the promise is awaited. – Bergi Apr 26 '20 at 17:48
  • I'm not sure what you mean by "*completed but unresolved promises*". – Bergi Apr 26 '20 at 17:50
  • 1
    Really, `async`/`await` is just sugar for `.then` calls, and promises are (mostly) just clever wrappers for callbacks. Do you understand how node.js event handling works in general, and could you answer your question if a callback was passed to `fetch` instead? – Bergi Apr 26 '20 at 17:51
  • @Bergi Ok. When I think of it in terms of callbacks, it makes more sense. The above would then just be `fetch('example.com', function(response) { ... 3 seconds work... })` – BryceLarkin Apr 26 '20 at 17:52
  • 1
    No, it would not. It would be `fetch('example.com', function(response) {}); ... 3 seconds work...` Only the code after the `await` is what would go into the callback. – Bergi Apr 26 '20 at 17:57
  • Got it. Thanks for clearing it up. – BryceLarkin Apr 26 '20 at 17:59

1 Answers1

1

here there is a great reading on the event loop.

In a high level perspective there is a forever running loop listening for events in the form of list of actions and processes them one at a time.

The code you shared above cannot be used globally due to the single threaded design of nodejs. What nodejs does is that every time you call a function it is pushed into the queue and processed. In every tick of the you pop something off the queue and run it or push something new.

In js world no 2 things can run at the same time. If you run a long running js code it blocks the entire system thus your system does not respond to new events since the eventloop is blocked.

That said there is another side of the story when you run all those nonblocking functions they are handled behind the scenes in v8 js engine. Since v8 native code it can make use of things like multiple threads and wait for things to finish or poll things checking whether they are ready and ones things are ready a new message gets inserted back into the js event loop and you get access to the data in js world.

So coming back to your question in an async function when you call await it really just means you yield the execution to the other things waiting in the message queue and once the thing you awaited gets ready it is pushed back to the queue and you continue from where you left of.

There is not really a waiting in the and the point of await is not to block the event loop with a blocking native function and to yield the execution to other things and you async function in the end of the day returns a Promise which only makes sense if you use .then and add a callback to be passed back to js world when the data is ready.

nikoss
  • 3,254
  • 2
  • 26
  • 40