Javascript Javascript函数在nodejs中返回未定义的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35308331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript function returning undefined value in nodejs
提问by iam
I am writing code for getting data.
First I call **getsomedata**function to get data and inside getsomedatafunction I am calling another function getRandomdatato get data and returning it back to the previous function but it is returning undefined. But in getRandomdatadata could be seen in console.log.
Do I need to use callbacks?
我正在编写用于获取数据的代码。首先,我调用**getsomedata**函数来获取数据,在getsomedata函数中,我调用另一个函数getRandomdata来获取数据并将其返回到前一个函数,但它正在返回undefined. 但在getRandomdata数据中可以看到console.log。我需要使用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;
}
回答by Rayon
Instead of
return, you should usecallbacksbecause inasynchronousoperations,returndoes not wait for theI/Ooperation to complete.
而不是
return,您应该使用,callbacks因为在asynchronous操作中,return不等待I/O操作完成。
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.
Callback- 在 JavaScript 中,高阶函数可以作为函数中的参数传递。由于 JavaSCRipt 是单线程的,一次只发生一个操作,每个将要发生的操作都在单线程中排队。这样,传递的函数(作为参数)可以在父函数 operation( async) 的其余部分完成时执行,脚本可以在等待结果的同时继续执行。
Usually this callbackfunction 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);
}
});
}

