Exploring the Functionality of Callbacks in Implementing Methods

I am curious about how we can call res.render from within a callback function.

function retrieveUsers(callback){
  fs.readFile('data.json', 'utf8', (err, data) => {
    if (err) return callback(err);
    const users = JSON.parse(data);
    return callback(null, users);
  });
}

app.get('/', (req,res) => {
  retrieveUsers((err,users) =>{
    if(err){
      res.render('error', {error:err})
    } else{
      res.render('index', {title: "users", users: users.users});
    }
  })
}); 

I'm puzzled by how the callback function is able to utilize the res.render method when it's outside of the actual route and the Callstack is filled with the getUsers(cb) function. How can I determine which values are accessible to callbacks in future scenarios?

Answer №1

When using inline functions/callbacks like in this example, they have access to the parent function scope along with all its variables and arguments, known as "lexical scope".

If the callback was not declared inline, it wouldn't have access to those variables/arguments because it would be in a different scope.

Therefore, the inline callbacks for readFile() can access cb(...), and similarly, the inline callback for getUsers() can access req and res since they are part of the parent scope.

It's important to note that this access is not from the call stack but rather from the lexical scope.


The ability to use res.render() only exists because the function is declared within the lexical scope where res is accessible. If the route were declared differently:

function getUsersCallback(err, users) {
    if(err){
      res.render('error', {error:err})
    } else{
      res.render('index', {title: "users", users: users.users});
    }
}

app.get('/', (req,res) => {
  getUsers(getUsersCallback);
}); 

In this case, it would not work because the callback function isn't declared in a lexical scope that has access to req and res.


Remember that function arguments and local variables in JavaScript are stored in scope objects, not on the call stack like in languages such as C/C++. This allows for function scopes to potentially exist beyond when the function returns (due to closures) and enables scopes to be chained for the interpreter to look up values in the parent scope to implement the "lexical scope" of the language.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What are the steps to make React JSX direct to "/profile" and display the profile page?

Throughout my application, I have integrated reach router to facilitate navigation between the different pages. However, I came across a navbar component that I really like and decided to add it to my app. Strangely, clicking on the "to: "/profi ...

Tips for including absent items in an array

Looking to fill in missing weeks and years in an array of objects that have a reportYear and reportWeek property. How can I insert additional objects to cover all weeks from the beginning date to the end date? For example: Input: [ {"reportYear":2017, ...

Mastering TypeScript in Router Configuration

I am currently working with a standard router setup. type Routes = '/' | '/achievements' | ... ; This helps in identifying the routers present in the project. However, I am faced with a new challenge of creating an array that includes ...

Unable to execute Javascript function within a click event handler

I am encountering an issue with a div that is loaded through ajax. Here is the structure of the div: <div id="container"> <a href="#" class="easyui-linkbutton submit_data">Click here to submit</a> </div> Within the same file c ...

Loading all assets in advance

Is there a one-size-fits-all method to preload all assets before using them? I need to load various images, audio files, and some .swf files before my application launches. Right now, I load images by creating new <img> elements with the image path a ...

What is the outcome when the return type is a string or a function that takes validation arguments and returns a string?

This excerpt is extracted from line 107 over here. From my understanding, it indicates: The function either returns a string directly or a function that accepts ValidationArguments as input and then produces a string output. Given that this is new to m ...

Revamping elements according to ordered array. Angular version 4.3

Dealing with an array of data that needs to be sorted for displaying in a component seems to be a challenge. Despite having a functional code sample demonstrating the concept, the sorting is not reflected in the Angular app's DOM. The original data i ...

Is there a way to dynamically import a JSON file within an ECMAScript module?

Currently, I am attempting the following in my code: let filePath = '../../data/my-file.json' import inputArray from filePath assert { type: 'json' } The outcome of this operation is as follows: file:///.../script.mjs:5 import inputArr ...

Showing different HTML elements based on the link that is clicked

Recently, I delved into the world of web development and decided to test my skills by creating a basic webpage with an interactive top navigation bar. Depending on the link clicked, specific HTML elements would be displayed while turning off others using a ...

Differences Between Using Array.push() and Literal (Bracket) Notation in JavaScript

I am looking at this specific answer. What is the reason behind Code Snippet 2 not producing the same result as Code Snippet 1? Code Snippet 1: var firstEvents = events.reduce(function(ar, e) { var id = e.getId(); if (e.isRecurringEvent() && ...

Smoother transitions with spline curves in Three.js

My current project involves drawing a CubicBezierCurve3 curve in three js. However, I want to enhance the drawing process by visualizing it as a moving rocket leaving behind a gas trail. Rather than having the entire curve drawn at once, I aim to draw it p ...

I'm wondering why myDivId.toggle() is functioning properly but myDivClass.toggle() is not working as expected

Using JQuery's toggle() function, I have been able to hide and show some DIVs successfully. Recently, I discovered relationships between certain DIVs that allowed me to group them into a class. I decided to try toggling the entire class rather than ...

What is the optimal method for transmitting data for a substantially large music playlist via HTTP?

I am currently in the process of developing an online music player. My main challenge lies in retrieving a comprehensive list of songs from the database and transmitting it to the user. The user should have the ability to create playlists on-the-go, hence ...

Error encountered while attempting to import external JSON data into SurveyJS

This Codepen example showcases SurveyJS using a simple JSON structure: var json = { "questions": [{ "type": "text", "title": "Test question 1", "name": "Test question" }, { "type": "comme ...

Obtaining the initial row information from jqGrid

If I use the getRowData method, I can retrieve the current cell content instead of the original data before it was formatted. Is there a way to access the original content before any formatting transformations are applied? Just so you know, I am filling t ...

Make the download window appear automatically when downloading a file

How can I use JavaScript/TypeScript to prompt the browser to open the download window? My goal is to give users the ability to rename the file and select the download folder, as most downloads are saved directly in the default location. This is how I curr ...

What is causing the issue with the minlength and maxlength validations not functioning correctly?

I am currently working with AngularJS and HTML to write code. I am facing an issue where the minlength and maxlength validations are not functioning properly in my code. Below is a snippet of my HTML code: <input type="text" id="healthcomplaint" ng-m ...

The close icon in the ReactStrap modal is not being displayed properly, and I need to know how to utilize the header with a different tag

I recently started working with React JS and I am currently utilizing the Modal component from reactStrap. <Modal isOpen={props.isOpen} centered='true'> <ModalHeader> Change this Question? <button type= ...

How can jQuery incorporate additional selection criteria for a parent element?

How can I apply a specific criteria to a node that has been selected using jQuery? let objDIV = $("#selindividual").parent(); After retrieving the DIV, I want to target the following: button:contains(\"Submit\") If I had to do this in ...

Access to this page via the Odesk API in node.js is restricted and unauthorized

/** * Here is an example of how to use the oDeskAPI * * @package oDeskAPI * @since 09/22/2014 * @copyright Copyright 2014(c) oDesk.com * @author Maksym Novozhylov <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data ...