Node.js Promise asynchronous execution Mysql query

Recently written logical Node.js a rear end, which involves linking table Mysql query (i.e. a query using a table to check the results of another table), then you need nested multilayer callback. So the question becomes, multi-nested callback function will have problems, "callback hell", the code structure is very ugly, and the Promise and solutions async is to solve the "callback hell" and produced. I chose the Promise, but encountered some problems in the code structure, and finally through the guidance of chiefs is resolved, hereby record.

You must first install mysql module (under the premise Mysql installed):

npm install -s mysql

Then execute (here's my mistake, correct later in the code):

/*
 注意这是一段错误的代码
*/
const mysql = require('mysql');
// 建立连接
let connection = mysql.createConnection({
  host: '127.0.0.1', // 数据库地址
  port: '3306', // 数据库端口
  user: 'root', // 用户名
  password: '', // 密码
  database: '' // 数据库名称
});
// Promise异步执行
let prom = Promise.resolve();
prom.then(function () {
  // 执行查询
  connection.query('SELECT * FROM users_table;', (e, result) => { // 这里users_table是我的表名,请读者自行替换
    console.log('Search complete!');
    if (e) {
      throw e;
    }
    connection.end(); // 关闭连接,否则程序不会自动退出
    return Promise.resolve(result);
  });
})
  .then(function (data) {
    console.log('Search result: ', data);
  });

Output:

Search result:  undefined
Search complete!

Can be seen, then () method to perform, and then return results mysql, mysql explain the asynchronous execution has not been monitored Promise, Promise that the program has already been implemented, so enter the next step.

After continuous learning Promise, Promise asynchronous execution to get the correct code is as follows:

const mysql = require('mysql');
// 建立连接
let connection = mysql.createConnection({
  host: '127.0.0.1', // 数据库地址
  port: '3306', // 数据库端口
  user: 'root', // 用户名
  password: '', // 密码
  database: '' // 数据库名称
});
// Promise异步执行
let prom = Promise.resolve();
prom.then(function () {
  // 执行查询
  return new Promise(resolve => {
    connection.query('SELECT * FROM users_table;', (e, result) => { // 这里users_table是我的表名,请读者自行替换
      console.log('Search complete!');
      if (e) {
        throw e;
      }
      connection.end(); // 关闭连接,否则程序不会自动退出
      resolve(result);
    });
  });
})
  .then(function (data) {
    console.log('Search result: ', data);
  });

result:

Search complete!
Search result:  [
  // some data
]

execution succeed.

After summary reflection, I understand Promise.then () is, then () method returns a Promise object, and then () method itself is not asynchronous, and did not listen asynchronous callback, so the real asynchronous function should be placed on return the Promise object, and the object Promise will resolve () method to monitor, implement asynchronous execution.

Published 19 original articles · won praise 0 · Views 1419

Guess you like

Origin blog.csdn.net/Z_ammo/article/details/103802234