Check out a free preview of the full The Hard Parts of Asynchronous JavaScript course
The "Calling the Outside World Q&A" Lesson is part of the full, The Hard Parts of Asynchronous JavaScript course featured in this preview video. Here's what you'd learn in this lesson:
Will answers questions from students about where Callback Queue is stored, how do calls to Browser APIs get handled if they are executed at the same time, size limit to the Callback Queue, and more.
Transcript from the "Calling the Outside World Q&A" Lesson
[00:00:00]
>> Will Sentance: Everybody's thumbs out. I have a clarification. I'm surprisingly clear, you lost me. All right, Victor has a clarification, Brian has one, Paul has one, Michelle has one, Mike. Mike why don't you kick us off, James has one, Mike why don't you kick us off?
>> Mike: I was just wondering where the callback queue, is that you're gonna store it in a web browser feature?
[00:00:18]
>> Will Sentance: That's a JavaScript feature. No, that's a JavaScript engine feature. Very, very good question. That's a JavaScript engine feature. Brian?
>> Brian: All right, how does it prioritize those? Like if you had more than ones that time out. They all were going up from 0 to 10 or something like that.
[00:00:35]
How would it-
>> Will Sentance: So, as soon as they complete, they ascend here. If they complete at the same time, eh.
>> Will Sentance: It's too subtle to, I don't know. I think at the same time, the scenario's improbable, Alec?
>> Alec: Is that a stack then? The call back queue?
>> Will Sentance: There's a queue.
[00:00:59]
>> Alec: Okay, but.
>> Will Sentance: It's a queue in a sense that we pass in, print hello. If we were to complete another web browser feature, and its web browser feature can be pass its function. It's going to be queued up behind, to start next. Okay, so it's a first in, first out.
[00:01:17]
So it's a queue structure.
>> Will Sentance: [LAUGH] No condescending giggles, Alec asked a very legitimate question, whether the word queue is stack. A very legitimate question. All right, who else had a, Victor was next.
>> Speaker 5: What does set interval do?
>> Will Sentance: Let's hold on, Sandra, we'll do it, yeah, it's a very interesting one.
[00:01:43]
But think of it as doing something very similar in the web browser. I'll come to that one on one with you, dude. There was other James?
>> Speaker 5: Yeah, I can't remember, does the task queue get processed at the end of the event loop, or at the start of, or sorry, the event loop iteration, or the start of the next iteration?
[00:02:02]
>> Will Sentance: Once you've finished all synchronous tasks.
>> Speaker 5: So at the end of the itteration.
>> Will Sentance: In that sense, yes. Let's just make sure we have any online questions we're answering. Paul, go ahead.
>> Speaker 6: I think I had one questions, I just want to make sure I know the set time out method that was provided by the browser environment.
[00:02:19]
>> Will Sentance: Yeah, yeah. Set time, go ahead please, go ahead man.
>> Speaker 6: So then the event loop and the call back queue aren't unique to each individual browser run time but say other non-browser run time etc would also have those-
>> Will Sentance: Absolutely, how Node implements these pieces that is somewhat up to them, so we'll come to a piece later on where node implemented in a somewhat different way.
[00:02:45]
But we'll see that a little bit later on, dude. But that's a great, great question. Yeah, Ben?
>> Ben: Is there a limit to this size of the callback queue?
>> Will Sentance: I mean, I assume so but that's a really good question. I would say test it out, and we'll talk about the, because there was a call stack of course, the callback queue.
[00:03:06]
I mean as long as, don't forget the callback queue, you're not storing this function here. You're referencing its position in JavaScript memories, so the question is really, is there a limit to JavaScript memory for function definitions? Do you see? You're not limited, you're not copying this function here, so to me, the question is, is there a limited Java Script run time memory?
[00:03:29]
Well, yes, but that implies for your callback queue, whatever that limit is, which is independent to the callback queue, does that makes sense Ben?
>> Ben: Yes.
>> Will Sentance: Good question though. Yes Michelle?
>> Michelle: I was just wondering if the event loop is also part of the JavaScript engine that you referenced?
[00:03:44]
>> Will Sentance: Mhmm, absolutely, but it's going to be even more stuff than just handle this, but we'll see those in a moment. Sean.
>> Sean: What happens when you pass in an anonymous function to the set time out?
>> Will Sentance: Anonymous functions, you can almost think of them being stored in memory as like unlabeled functions.
[00:04:01]
But they still have a position and address in memory. Think of we're not passing the label to the function really. Under the hood, what we're passing is a reference or particular position in memory. And so, if we don't have a label for print hello, we pass a function indirectly.
[00:04:16]
That function is still being defined in global memory. When I run set timeout and pause for functioning, not a labelled functioning. But when I define right there in my parentheses, that is still being stored in global memory just without a label. And it's that referenced function that's eventually being referenced here.
[00:04:36]
This doesn't have a label now, but it's still referencing the position in Java Script's global memory, where that function was defined. Makes sense? Yes.
>> Mike: Good question.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops