Returning a Value from Callback Function in Node.Js

Returning a value from callback function in Node.js

Its undefined because, console.log(response) runs before doCall(urlToCall); is finished. You have to pass in a callback function aswell, that runs when your request is done.

First, your function. Pass it a callback:

function doCall(urlToCall, callback) {
urllib.request(urlToCall, { wd: 'nodejs' }, function (err, data, response) {
var statusCode = response.statusCode;
finalData = getResponseJson(statusCode, data.toString());
return callback(finalData);
});
}

Now:

var urlToCall = "http://myUrlToCall";
doCall(urlToCall, function(response){
// Here you have access to your variable
console.log(response);
})

@Rodrigo, posted a good resource in the comments. Read about callbacks in node and how they work. Remember, it is asynchronous code.

Returning Value From Callback Function In NodeJS For Redis

This is where you'd use the Promise constructor with the resolve/reject callbacks:

CoreHelpers.controller("get.alerts", async () => {

const promise = new Promise((resolve, reject) => {
RedisClients.client0.lrange("alerts", 0, -1, (err, reply) => {
if (!err) {
resolve(reply);
} else {
reject(err);
}
});
});

return promise.then(value => {
// If you want to modify the value first
return value.map((x: any) => JSON.parse(x));
});
});

Return value from callback in function

Do you get anything back when you console.log your return?
for example:

function dbCall(){    
database.users.get("test-username",function(err, value){
if(err){
return false;
}else{
console.log(value);
return value;
}
}
}

if you get something in value then the reason you don't see anything in return is because the api call is asynchronous. You'll have to make a promise.

new Promise(resolve,reject){
let data = dbCall();
if(data == false)
reject(data);
else
resolve(data);
}

return value of a callback function in Node.js

fs is an Asynchronous function, it doesn't return the values as it doesn't know when the value will be available due to which it logs undefined.

When you say console.log(fs.readdir()), it reads the fs function and checks if it is returning anything which in this case is undefined and hence logs it. The execution didn't go inside the callback of fs.readdir() yet as it is asynchronous as takes time for that to complete.

fs.readdir() is like a promise, it calls to read the directory and forget about it till the reading operation is done and after which it's callback function is called.
Hence, we pass callback functions which will be called back when the asynchronous operation is done executing like below :

function readFile (callback){
fs.readdir("ab",function(err,data){
return callback(data);
});
}

readFile(function (data){
console.log(data);
});

How to return value from callback function?

You could create a custom "Promisify".

E.g:

function storeData(data) {
return new Promise((resolve, reject) => {
const id = "5f354b7470e79f7e5b6feb25";

const body = { name: "john doe" };

bucket.insert(id, body, (error, result) => {
if (error) reject(error)

resolve(result)
});
})
}


Related Topics



Leave a reply



Submit