Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Empowering you to understand your world

Node.js Code Example: How To Get A Value From An Asynchronous Function In Node.js

Screenshot of a callback function returning a value
A screenshot of a callback function returning a value in Node.js (in the WebStorm IDE).

If you’re trying to get a return value from an asynchronous callback function in Node.js before you continue execution, there is a way to do it without having to use async.wait or convert it to a synchronous function.

You would pass a function (in this code sample, the function is named ‘callbackfunc’) containing the code you want executed after the asynchronous operation is complete as a parameter to that function as shown below.

In this case, the asynchronous operation is a MongoDB database query that pulls up the record of a certain user. The function being passed around (‘callbackfunc’) is highlighted in purple.

GetRecord('nicholasbrown29', function(returneddata)  {
    console.log('Variable contents: ' + returneddata); //returneddata contains the parameter passed by the 'callbackfunc' call below.
});
//Asynchronous: Get a record from a MongoDB database.
function GetRecord(nameofuser, callbackfunc) {
    Record.find({UserName: nameofuser}, function (error, datatoreturn) {
        callbackfunc(datatoreturn); //Executes the callback function found in the GetRecord method call above.
    });
}

This is also useful if you want to write a getter function that gets data from a PostgreSQL database, ensuring that your getter code is executed only after the database query operation is complete. This will enable you to avoid ‘undefined’ return values because your code executed before the database operation is completed.

Leave a Reply

Subscribe to our newsletter
Get notified when new content is published