12

I am writing code for getting data. First I call **getsomedata** function to get data and inside getsomedata function I am calling another function getRandomdata to get data and returning it back to the previous function but it is returning undefined. But in getRandomdata data could be seen in console.log. Do I need to use callbacks ?

router.get('/get-data', function (req, res, next) {
    var result = getsomedata(some_parameter);
    console.log(result);   // receiving undefined
    res.send(result);
});

function getsomedata(some_parameter_recieved) {
    var getsomedata = getRandomdata(random_params);
    console.log(getsomedata);    // receiving undefined
    return getsomedata;
}

function getRandomdata(random_params_recieved) {
    // after some calculation 
    console.log(random_data);           // receiving proper data
    return random_data;
}
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
iam
  • 873
  • 3
  • 9
  • 19
  • 4
    Welcome to `asynchronous` programming! Read about `callbacks` or `Promise` – Rayon Feb 10 '16 at 06:31
  • You probably need to return values from the `callback`. Try this http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/ – Jeff P Chacko Feb 10 '16 at 06:38
  • Possible duplicate of [How to return value from an asynchronous callback function?](http://stackoverflow.com/questions/6847697/how-to-return-value-from-an-asynchronous-callback-function) – Gavriel Feb 10 '16 at 06:38
  • yaha sure, but please can you make modify of this code? because its argent to me. if you don't mind. – iam Feb 10 '16 at 06:42

1 Answers1

33

Instead of return, you should use callbacks because in asynchronous operations, return does not wait for the I/O operation to complete.

Callback - In JavaScript, higher order functions could be passed as parameters in functions. Since JavaSCript is a single threaded, only one operations happens at a time, each operation thats going to happen is queued in single thread. This way, passed functions(as parameter) could be executed when rest of the parent functions operation(async) is completed and script can continue executing while waiting for results.

Usually this callback function is passed in as the last argument in the function.

Using Callbacks:

router.get('/get-data', function(req, res, next) {
  getsomedata(some_parameter, function(result) {
    console.log(result);
    res.send(result);
  });
});

function getsomedata(some_parameter_recieved, callback) {
  getRandomdata(random_params, function(random_data) {
    callback(random_data);
  });
}

function getRandomdata(random_params_recieved, callback) {
  // after some calculation
  callback(random_data);
}

Using Promise:

router.get('/get-data', function(req, res, next) {
  getsomedata(some_parameter, function(result) {
    console.log(result);
    res.send(result);
  });
});

function getsomedata(some_parameter_received, callback) {
  getRandomdata(random_params).then(function(random_data) {
    callback(random_data);
  }).catch(function(e) {
    //handle error here
  });
}

function getRandomdata(random_params_received, callback) {
  return new Promise(function(resolve, reject) {
    // after some calculation
    if (RandomDataGeneratedSuccessfully) {
      resolve(random_data);
    } else {
      reject(reason);
    }
  });
}
Rayon
  • 34,175
  • 4
  • 40
  • 65