Look at the code first:
functionSelect (Sqlscript) {
varresult = "";
Sql.connect (config,function(ERR) {
varRequest =NewSql. Request ();
Request.query (Sqlscript,function(Err, Recordset) {
result = Recordset;
Console.log ("AAA:" +result);
});
Request.on (' Recordset ',function(columns) {
//emitted once for each recordset in a query
});
Request.on (' Row ',function(ROW) {
//emitted for each row in a recordset
});
Request.on (' Error ',function(ERR) {
//May be emitted multiple times
});
Request.on (' Done ',function(returnvalue) {
//Always emitted as the last one
});
});
Console.log ("BBB:" +result);
returnResult
}
After running can see, console.log ("AAA:" +result); This sentence still has the value, but this sentence console.log ("BBB:" +result); But it is: bbb:undifined. What is this for?
Originally
In general, the logic of the callback function is to first execute the first console.log, then the second Console.log, but the middle callback function, such as IO The operation is not actually executed, so console.log execution, the callback function is not executed, so there is no return value wait until the database returns data, the return content is not captured, this is The asynchronous nature of the process to control the trouble.
So
With Node can only take the following steps to write in the callback function is continued to invoke and execute can be used promise, generator and other ways to get more accustomed to the wording, then hurriedly learn promise, generator use it.
Node. js function return value