0

I've a return type function:

var responseData = '';
function getResponseFromKey(key){

    client.get(key, function(err, reply){
        responseData = reply;
    });
    return responseData;
}

When I call this function first time it returns blank and then second time returns my value what I want after running again.

I'm calling this function to print in html page.

http.createServer(function(request, response){
    response.writeHead(200, {'Content-Type':'text/plain'});
    getResponseFromKey("my_key");   
    console.log(responseData);
}).listen(8083);

As I'm familiar with node the function is going in asynchronous way. Can you please help me to make synchronous way? Do I need to use generators in that case?

Help would be appreciated!

3

3 Answers 3

1

You can use callback, promise or generator.

Using callback, you need to send a callback function and call it instead of returning a value

function getResponseFromKey(key, callback){
  client.get(key, function(err, reply){
    callback(reply);   
  });
}

http.createServer(function(request, response){
  response.writeHead(200, {'Content-Type':'text/plain'});
  getResponseFromKey("my_key", function() {
    console.log(responseData);
  });   
}).listen(8083);

Using promise, you need to return a Promise

function getResponseFromKey(key) {
  return new Promise(function(resolve, reject) {
    client.get(key, function(err, reply) {
      return resolve(reply);   
    });
  })
}

http.createServer(function(request, response){
  response.writeHead(200, {'Content-Type':'text/plain'});
  getResponseFromKey("my_key").then(function(responseData) {
    console.log(responseData);
  });   
}).listen(8083);

If you are using and up-to-date version of nodejs, you can use arrow functions, to make your code more readable.

Sign up to request clarification or add additional context in comments.

Comments

0

Your client.get function is async try returing return responseData; inside the client.get body

var responseData = '';
 function getResponseFromKey(key ,cb){

 client.get(key, function(err, reply){
    responseData = reply;
    return cb(responseData);
 });

}

http.createServer(function(request, response){
 response.writeHead(200, {'Content-Type':'text/plain'});
 getResponseFromKey("my_key", function(responseData) {
  console.log(responseData);
});   
}).listen(8083);

3 Comments

I'm getting this error while running your code TypeError: cb is not a function
have you written this correcty getResponseFromKey("my_key", function(responseData) { console.log(responseData); });
callback is the second parameter in a function call.
0

In many case, the callback is not a good choose.

You can use async,promises,generators and async functions which supported in es7.

I recommend promises beacuse the promise a key features in ES6.

I think this article will help you.

https://blog.risingstack.com/node-js-async-best-practices-avoiding-callback-hell-node-js-at-scale/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.