[英]Javascript function returning undefined value in nodejs
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
.首先,我调用
**getsomedata**
函数来获取数据,在getsomedata函数中,我调用另一个函数getRandomdata来获取数据并将其返回到前一个函数,但它返回undefined
。 But in getRandomdata
data could be seen in console.log
.但是在
getRandomdata
数据可以在console.log
看到。 Do I need to use callbacks
?我需要使用
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;
}
Instead of
return
, you should usecallbacks
because inasynchronous
operations,return
does not wait for theI/O
operation to complete.您应该使用
callbacks
而不是return
,因为在asynchronous
操作中,return
不会等待I/O
操作完成。
Callback
- In JavaScript, higher order functions could be passed as parameters in functions. Callback
- 在 JavaScript 中,高阶函数可以作为函数中的参数传递。 Since JavaSCript is a single threaded, only one operations happens at a time, each operation thats going to happen is queued in single thread.由于 JavaSCRipt 是单线程的,一次只发生一个操作,每个将要发生的操作都在单线程中排队。 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.这样,传递的函数(作为参数)可以在父函数操作(
async
)的其余部分完成时执行,脚本可以在等待结果的同时继续执行。
Usually this callback
function is passed in as the last argument in the function.通常这个
callback
函数作为函数的最后一个参数传入。
Using Callbacks
:使用
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
:使用
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); } }); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.