Javascript函数在nodejs中返回未定义的值

Javascript函数在nodejs中返回未定义的值

问题描述:

我正在编写获取数据的代码。
首先我调用 ** getsomedata ** 函数获取数据并在 getsomedata 函数内调用另一个函数 getRandomdata 获取数据并将其返回到上一个函数,但它返回 undefined 。但是在 getRandomdata 中可以在 console.log 中看到数据。
我是否需要使用回调

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;
}



而不是返回,你应该使用回调,因为在异步操作, return 不等待 I / O 操作完成。

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

回调 - 在JavaScript中,高阶函数可以作为函数中的参数传递。由于JavaSCript是单线程,因此一次只发生一个操作,每个将要发生的操作都在单个线程中排队。这样,当完成其余的父函数操作( async )并且脚本可以在等待结果时继续执行时,可以执行传递的函数(作为参数)。

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);
    }
  });
}