I think this is a really good summary. Of course, limiting yourself to synchronous code is, well, limiting. Things happen concurrently, and we need to be able to program in a world of concurrency.
When I/O occurs, with a callback, the event loop puts the rest of the computation in an area for things waiting for events (event handlers). When the I/O completes, the event handler is called to continue the computation.
Great question! Yes, you're on the right track. In CS 240, you learned about stacks. One way to deal with concurrency is multiple stacks. This works great and has a lot of power.
However, stacks require more memory than an event-loop architecture, which is a different way of handling concurrency (but limited mostly to I/O).
A thread is a computation with a stack, so you can have concurrent or even simultaneous computations.
Let's look at that. It's just before the section on event loop
It does make sense. If something depends on the thing being finished, we need to make sure it's in the (1) callback or (2) in the .then()
or (3) after the await
.
Sure. We'll go over all of them. They are all the same idea in different syntaxes.
Yes, let's do that with the question assignment.
It's an object that the browser creates to represent the response from the server. The response will typically contain some data, maybe with headers and such.
async/await is the newest, shiniest version. It makes programming much easier, but it does sacrifice some concurrency. (For example, you might want to make several simultaneous requests.) We won't worry about that scenario until much later, if ever, so for CS 304, we can focus on await.
The demo earlier should help. The then
attaches a
function (an event handler) to be called when/if the promise is
fulfilled. async/await
does a similar thing, but with
the rest of a function:
f(y)
has to wait
until the I/O completes. So, the rest of the function is like the
code in a .then()
.
Great point. It's just an alternative.
For sure!
Yes.
We will. Our programming interface with MongoDB will use await
.