First, as we all know, node is based on an event-driven asynchronous I/O architecture, so-called asynchronous is non-blocking, and it's plain that an event executes, I don't have to wait for it to finish before I can execute the next event. So the module in the node environment is basically asynchronous, the previous article said I used the Easymysql module in the project instead of the MySQL module, two modules for the operation of the query is asynchronous, so to implement nested query is often very cumbersome, and it is very likely to error. For this reason, in order to achieve query synchronization, I introduced asynchronous Process Control Async module, so that JS asynchronous operation into synchronous operation, so that on the one hand to facilitate reading comprehension, on the other hand can be very good to achieve the goal of demand, pro-test effective ~
Update.js file
1' Use strict '2 varAsync = require (' async ');3 varClient = require (' Easymysql ');4 varMySQL =client.create ({5' MaxConnections ': 106 })7 Mysql.addserver ({8Host: ' localhost ',9User: ' Test ',TenPassword: ' 123456 ', OneDatabase: ' Database ' A }) - -Exports.match =function(req, res) { the //get all the recording information you need - functionGetInfo () { - return NewPromise (Resolve, reject) = { -Mysql.query (' SELECT ' index ', ' name ' from TableA ',function(err, result) { + if(err) { - Console.log (err); +}Else { A resolve (result); at } - }) - - }) - } - in //update the name of each item to GDT - functionUpdateName (info) { to return NewPromise (Resolve, reject) = { + varindex = info[' index ']; - varsql = "UPDATE ' TableA ' SET ' TableB '. ' Name ' = ' GDT ' WHERE ' TableA '. ' Index ' = ' +index; theMysql.query (SQL,function(err, result) { * if(err) { $ Console.log (err);Panax Notoginseng}Else { -Resolve (' Update succeeded '); the } + }) A }) the } + -AsyncfunctionUpdate () { $Let result = '; $ //filter out the global UID and its corresponding index in the Gang statement - varGetinforesult =await GetInfo (); - for(Let i = 0; i < getinforesult.length; i++) { the varUpdateresult =await UpdateName (Getinforesult[i]); - Console.log (updateresult);Wuyi } the returnResult? Result: ' Success '; - } Wu -Update (). Then (function(Result) { About Console.log (result); $ - //Disconnecting database Connections - connection.end (); - }) A}
. babelrc files (using ES7 async/await requires Babel transcoding)
{ "presets": [ "stage-0" ], "plugins": [" Transform-async-to-generator "]}
The above code is just an example, mainly to illustrate the use of async, and can not directly run, the implementation of the requirements should be easy to understand, is found in table tables so the record index and name field, stored in
Getinforesult variable, and then the variable iterates through each item to update the operation. Because of the async process Control, GetInfo () and UpdateName (Getinforesult[i]) two asynchronous operations are changed to synchronize so that the program runs without error for asynchronous reasons.
For more details on async you can find a tutorial on the great god of Ruan Yi: asynchronous operations and Async functions
Using asynchronous processes in Nodejs to control async