return 函数的值始终未定义且顺序
return value from function is always undefined and order
我尝试创建一个生成随机数的函数并验证它是否尚未使用,然后return它。
但是我得到了一个未定义的结果,我首先得到了这个函数中针对 console.log 的函数的结果,而它应该是相反的。
// function main()
console.log('The result of the function in the main() is ' + Bank_generateAccountNumber());
// function Bank_generateAccountNumber()
function Bank_generateAccountNumber()
{
var account_number = Math.floor((Math.random() * 8999) + 1000);
console.log('Bank_generateAccountNumber trying with this number: ' + account_number);
bdd.query('SELECT * FROM bank_accounts WHERE account_number = ?', gm.mysql.escape(account_number), function(e, d, f)
{
if(!d.id)
{
console.log("this number is available ! " + account_number);
return account_number;
}
console.log("this number is already used ! " + account_number);
Bank_generateAccountNumber();
return 0;
});
}
我在写这篇文章时看到了,即使我没有连接到 mysql,我也收到了 "the result of the function in the main() is undefined",并且在收到错误消息后因为 "d.id" 是未定义。
我想先得到console.log(在函数中),然后得到函数的结果。
你有什么想法吗?
谢谢
我修改了您的函数以使用 Q 库使用延迟承诺。正如 Naresh Walia 在评论中所写,你有不止一个图书馆可以这样做:
var q = require('q');
Bank_generateAccountNumber().then(function(response) {
console.log('The result of the function in the main() is ' + response);
})
function Bank_generateAccountNumber() {
var response = q.defer();
var account_number = Math.floor((Math.random() * 8999) + 1000);
console.log('Bank_generateAccountNumber trying with this number: ' + account_number);
bdd.query('SELECT * FROM bank_accounts WHERE account_number = ?', gm.mysql.escape(account_number), function(e, d, f) {
if (!d.id) {
console.log("this number is available ! " + account_number);
response.resolve(account_number);
}
console.log("this number is already used ! " + account_number);
Bank_generateAccountNumber();
});
return response.promise;
}
我尝试创建一个生成随机数的函数并验证它是否尚未使用,然后return它。
但是我得到了一个未定义的结果,我首先得到了这个函数中针对 console.log 的函数的结果,而它应该是相反的。
// function main()
console.log('The result of the function in the main() is ' + Bank_generateAccountNumber());
// function Bank_generateAccountNumber()
function Bank_generateAccountNumber()
{
var account_number = Math.floor((Math.random() * 8999) + 1000);
console.log('Bank_generateAccountNumber trying with this number: ' + account_number);
bdd.query('SELECT * FROM bank_accounts WHERE account_number = ?', gm.mysql.escape(account_number), function(e, d, f)
{
if(!d.id)
{
console.log("this number is available ! " + account_number);
return account_number;
}
console.log("this number is already used ! " + account_number);
Bank_generateAccountNumber();
return 0;
});
}
我在写这篇文章时看到了,即使我没有连接到 mysql,我也收到了 "the result of the function in the main() is undefined",并且在收到错误消息后因为 "d.id" 是未定义。
我想先得到console.log(在函数中),然后得到函数的结果。
你有什么想法吗? 谢谢
我修改了您的函数以使用 Q 库使用延迟承诺。正如 Naresh Walia 在评论中所写,你有不止一个图书馆可以这样做:
var q = require('q');
Bank_generateAccountNumber().then(function(response) {
console.log('The result of the function in the main() is ' + response);
})
function Bank_generateAccountNumber() {
var response = q.defer();
var account_number = Math.floor((Math.random() * 8999) + 1000);
console.log('Bank_generateAccountNumber trying with this number: ' + account_number);
bdd.query('SELECT * FROM bank_accounts WHERE account_number = ?', gm.mysql.escape(account_number), function(e, d, f) {
if (!d.id) {
console.log("this number is available ! " + account_number);
response.resolve(account_number);
}
console.log("this number is already used ! " + account_number);
Bank_generateAccountNumber();
});
return response.promise;
}