function return value for node. js

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.