Check out a free preview of the full Serverless with AWS Lambda course

The "Node Lambda Context" Lesson is part of the full, Serverless with AWS Lambda course featured in this preview video. Here's what you'd learn in this lesson:

Scott shows the lambda continues to run if there's an async task left. This behavior can be disabled this behavior using the context object.

Preview
Close

Transcript from the "Node Lambda Context" Lesson

[00:00:00]
>> Instructor: So here's a function. You can see it's literally a handler. It takes an event and a callback. Okay, this is advance, man, I don't wanna talk about this. Okay, I'm gonna talk about it. This right here is a node-specific thing in Lambda, only for a node, because it's asynchronous.

[00:00:17]
This basically means, so by default, Lambda will wait until the event loop is empty before it decides to exit. Who here doesn't know what the event loop is? So I can describe that. It's totally fine, it's an advanced thing if you don't know what the event loop is, especially if you don't know JavaScript.

[00:00:33]
An event loop is basically how JavaScript does asynchronous things. An event loop, you can think of it like, JavaScript is constantly going from top to bottom to run synchronous operations. When it comes across on the asynchronous, it puts it in a table to be processed later at the bottom of this synchronous test, and it continues on that process until there is nothing registered, right, so that's an event loop and it keeps doing that.

[00:00:58]
By default, the Lambda is going to continue to be open until there's nothing left on that asynch table basically. This tells it to ignore that. Just when I call done, be done. When I run this call back, just cut it off, I don't care if there's anything hanging in the background.

[00:01:17]
This is useful for database connections. So if you connect to something pretty much anything other than dynamo DV and Lambda, which is why I did not talk about databases in this course, because it's extremely complicated. You will have to do this, otherwise a database connection is a long lift connection and if you call the call back inside of a lambda, it still won't exit because there's still asynchronous things hanging there at the background, your database connection.

[00:01:41]
This tells Lambda to ignore that database connection and exit anyway. So very complicated, that's what that first line is. And then if we just look, you can see it's kinda just. I mean, what is it doing here? It looks like it's making a request object. It's waiting for the next tick.

[00:01:59]
Yeah, it's basically just simulating requests, I mean, I'm sorry, it's just simulating express. It's pretty simple here. So yeah, nothing too fancy, but yeah, that's basically what it's doing. It's satisfying whatever expressed needs, but in the context of the Lambda's arguments.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now