admin管理员组文章数量:1318977
I just started working on node.js and getting to know its concepts, I am having a little trouble understanding callbacks,what I am trying to do is call function getUserBranch() in the function getOffers().
I read that because of node.js async nature its better to use a callback function to get the desired data once the plete execution is done.
Now I am having trouble retrieving the value that getUserBranch is returning,I have no proper idea how to do it, well the callback function has the value but how do I get the value from there?
file2.js
var getUserBranch = function(email,callback) {
client.execute('SELECT * from branch WHERE email=?', [ email ], function(
err, data, fields) {
if (err)
console.log("error");
else
console.log('The solution is in branch: \n', data);
res = data.rows[0];
return callback(res);
});
}
file1.js
var getOffers = function (email) {
var branchObj = require('./file2.js');
var branchList = branchObj.getUserBranch(email,getList));
return branchList;
};
var getList = function(res){
var results=res;
return results;
}
I just started working on node.js and getting to know its concepts, I am having a little trouble understanding callbacks,what I am trying to do is call function getUserBranch() in the function getOffers().
I read that because of node.js async nature its better to use a callback function to get the desired data once the plete execution is done.
Now I am having trouble retrieving the value that getUserBranch is returning,I have no proper idea how to do it, well the callback function has the value but how do I get the value from there?
file2.js
var getUserBranch = function(email,callback) {
client.execute('SELECT * from branch WHERE email=?', [ email ], function(
err, data, fields) {
if (err)
console.log("error");
else
console.log('The solution is in branch: \n', data);
res = data.rows[0];
return callback(res);
});
}
file1.js
var getOffers = function (email) {
var branchObj = require('./file2.js');
var branchList = branchObj.getUserBranch(email,getList));
return branchList;
};
var getList = function(res){
var results=res;
return results;
}
Share
Improve this question
asked Oct 6, 2015 at 18:18
marilynmarilyn
4013 gold badges5 silver badges18 bronze badges
1 Answer
Reset to default 3In async call work with callback function in the stack. Look this:
var getUserBranch = function(email,callback) {
client.execute('SELECT * from branch WHERE email=?', [ email ], function(err, data, fields) {
if (err)
console.log("error");
else{
console.log('The solution is in branch: \n', data);
/* This data stack 3 */
callback(data.rows[0];);
}
});
};
var getOffers = function (email, callback) {
var branchObj = require('./file2.js');
branchObj.getUserBranch(email, function(data){
/* This data stack 2 */
callback(data);
});
};
function anyFunction(){
getOffers("[email protected]", function(data){
/* This data stack 1 */
console.log(data);
});
}
本文标签: javascriptNodejs get the return value of a functionStack Overflow
版权声明:本文标题:javascript - Node.js get the return value of a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742057263a2418369.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论