1

This following JavaScript snippet works fine:

// generate next extry in order to upadate the
// country html table
app.get('/next', (req, res, next) => {
  data.nextEntry()
    .then(country => tpl.salesForCountry(country))
    .then(html => res.send(html))
    .catch(e => next(e))
})

However, I am asking myself why?

When the get route is called, the Lambda function executes and my custom function nextEntry (it's async) generates a new json object which is then converted to HTML and finally sent back to the requester by calling send of the response object (res).

But why is the reference of res still available and not undefined? Because the Lambda function execution (provided to the get route) has finished long before the HTML has been generated by the then branches. Could somebody kindly point me to the right direction - THX

edi
  • 917
  • 7
  • 17
  • 2
    It's a question I think most of us had when we started out with JavaScript (or other languages with closures). See the linked question's answers for details, or my **very** old blog post [*Closures are not complicated*](http://blog.niftysnippets.org/2008/02/closures-are-not-complicated.html). The function you're passing `then` has a link to ("closes over") the environment where it was created. That environment contains `req`, `res`, `next`, and a few other things. That link is what makes it possible for the function (a *closure*) to use them when your `get` handler has returned. – T.J. Crowder Mar 23 '22 at 09:20
  • Ah, yes, of course. I know about closures, however, I havn't realized that app & get are forming such. Many Thanks! If you'd post your comment as answer, I'd accept it as correct. – edi Mar 23 '22 at 10:58
  • It's not `app` and `get` that are doing it. It's just fundamental to how JavaScript works. Any time you create a function, it closes over the environment where it's created. There's nothing that `app.get` does that specifically enables that behavior, it's intrinsic. (Express could make the objects invalid after the call returned, but of course it's designed to work in Node.js, which is inherently asynchronous, so that would be a silly thing to do. :-) ) – T.J. Crowder Mar 23 '22 at 12:04
  • Yes, you are right. I've been writing to quickly. But if I'm not mistaken the provided lambda function (with params req, res, next) actually encloses the `nextEntry` call. Therefor, its parameters are still available, even though the execution of the lambda function has already finished. – edi Apr 07 '22 at 13:38
  • The call to `nextEntry` is inside the `get` callback, yes. But that's not the closure bit. (Closures are created when functions are *created*, not when they're called.) The closures that are relevant are the functions being created and passed to `then` and `catch`. Those functions close over the environment for the call to the `get` callback, so they have access to the content of that environment (`req`, `res`, and `next` primarily). :-) – T.J. Crowder Apr 07 '22 at 14:03

0 Answers0