3

I am new to callback functions. Actually I am using request module in node js to fetch some info. I am confused how to return my response as Javascript is asynchronous in nature. I have the following codes. My app.js code is as follows:

var express = require('express');

var app = express();

const request = require('request');

var call = require('./method');
app.use(express.json());

app.post('/call', function(request, response){
    var json = request.body;
    var p1 = json.p1;
    var p2 = json.p2;
    var p3 = json.p3;
    var p4 = json.p4;
    call.callApi(p1, p2, p3, p4);

    //response.send(request.body); 
});
app.listen(3000);

And my function file named as method.js is as follows:

const request = require('request');

function callApi(p1, p2, p3, p4, callback) {

    var api = "http://test.roadcast.co.in:8088/api/route/" + p1 + "/" + p2 + "/" + p3 + "/" + p4;
    console.log(api);
    request(api, function(err, res, body) {
        var result = body;
        return callback(result, false);
        //console.log(body);
        //return result;
    });
}

module.exports = {
    callApi: callApi,
};

And I am request a POST request by sending a json data in body as follows:

{ "p1":"28.7033129", "p2":"77.1542682", "p3":"28.709637", "p4":"77.291254" }

I am getting the results properly but just I want to return the reponse, please correct me where I am getting wrong.

4
  • Sir to whom you want to return response ? any function or user browser/client or what? Commented Jun 5, 2018 at 11:15
  • In the app.js file. Commented Jun 5, 2018 at 11:16
  • you call using call.callApi(p1, p2, p3, p4);, and your callApi is declared function callApi (p1, p2, p3, p4, callback) { ... so, simply pass a callback function when you call the function Commented Jun 5, 2018 at 11:17
  • a hint: nothing wrong with what you've written in method.js ... but the "node convention" for callbacks is the other way around ... you do callback(result, false); - the "convention" would be callback(false, result); - i.e. fn(error, result) - it's not important, but just thought I'd mention it Commented Jun 5, 2018 at 11:18

2 Answers 2

4

Just like any other callback. In your code, you weren't passing anything for the callback parameter. If you do that, and use its argument, you can write the response. It's fine not to write the response synchronously during the post callback (fine and completely normal).

E.g.:

app.post('/call', function(request, response){
    var json = request.body;
    var p1 = json.p1;
    var p2 = json.p2;
    var p3 = json.p3;
    var p4 = json.p4;
    call.callApi(p1, p2, p3, p4, function(result) { // ***
        response.send(result);                      // ***
    });                                             // ***
});

That said, you should change your callback to match the standard Node callback style (or adopt promises, which would be even better): Have it pass an error or null as the first argument, and the result (if any) as the second. E.g.:

function callApi(p1, p2, p3, p4, callback) {

    var api = "http://test.roadcast.co.in:8088/api/route/" + p1 + "/" + p2 + "/" + p3 + "/" + p4;
    console.log(api);
    request(api, function(err, res, body) {
        return callback(err, body); // ***
    });
}

then

app.post('/call', function(request, response){
    var json = request.body;
    var p1 = json.p1;
    var p2 = json.p2;
    var p3 = json.p3;
    var p4 = json.p4;
    call.callApi(p1, p2, p3, p4, function(err, result) { // ***
        if (err) {                                       // ***
            // Send error response                       // ***
            // ...                                       // ***
        } else {                                         // ***
            // Send successful response                  // ***
            response.send(result);                       // ***
        }                                                // ***
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @T.J Crowder, I was new to callback function.
4

You just need to pass callback function as you are calling callback function inside callApi but you are not passing any callback to callApi.But always remember keep err as the first argument in the callback function.

Example:

app.post('/call', function(request, response){
var json = request.body;
var p1 = json.p1;
var p2 = json.p2;
var p3 = json.p3;
var p4 = json.p4;
call.callApi(p1, p2, p3, p4, function(err,result) {

     if(!err) 
        response.send(result);
    else {
      // do something
    }                      
});                                          

});

and your callApi function should be like this:

   function callApi(p1, p2, p3, p4, callback) {

    var api = "http://test.roadcast.co.in:8088/api/route/" + p1 + "/" + p2 + "/" + p3 + "/" + p4;
    console.log(api);
    request(api, function(err, res, body) {
          var result = body;
         //console.log(body);
         if(!err)
         callback(null,result);
         else {
          callback(err);
          }


    });
}

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.