Mysql: Asynchronous ... Part 2 !

Created on 7 Apr 2016  路  2Comments  路  Source: mysqljs/mysql

This is another question, but I didn't see were I can put the question flag.

I have multiple files:

  • app.js
  • init.js
  • sql_functions

app.js

var init = require("./init.js");
var sql_functions = require("./sql_functions.js");
var return_value = 0;

// Call function from 'init.js'
init.isTableExist(connection);

// Executed at the same time than above.
sql_functions.closeDB (connection);

_init.js_

module.exports.isTableExist = function(connection) {
  var return_value = -1;

  if(sql_functions.connect(connection) == 0){

    // Try to determinte if table 'product' exist
    connection.query("SELECT * FROM product;", function(err, rows, fields) {
      if (err) {
        console.log('Table doesn\'t exist.\n');
        return_value = createTable(connection); // 0 = Ok  |  1 = Error

      }else {
        console.log('Table "produit" exist.\n');
        // console.log(rows);
        return_value = 0;
      }
    });

  }
  return return_value;
}

sql_functions.js

// Connect to the DB
module.exports.connect = function(connection) {
  try{
    connection.connect(connection);
    console.log('Succefully connected to the database.\n');
    return 0;
  } catch(e){
    console.log('/!\\ Cannot connect to the database, with the following error :');
    console.log(e);
    return -1;
  }
}

// Close the connection
module.exports.closeDB = function(connection) {
// function closeDB(connection){
  try{
    connection.end();
    return 0;
  } catch(e){
    console.log('/!\\ Cannot close the connection to the database !\n');
    return -1;
  }
}

If I do that in app.js, isTableExist is called, and before it's finished, sql_function.closeDB(connection); is called to.

So it's totaly random when I put multiple lines in app.js ? Or I'm forced to close the connection in init.js and open another one after calling isTableExist ?

question

Most helpful comment

same approach, again - you "return" values by passing them to callback function as a parameter:

module.exports.isTableExist = function(connection, cb) {
    connection.query("SELECT * FROM product;", function(err, rows, fields) {
      if (err) {
        console.log('Table doesn\'t exist.\n');
        return_value = createTable(connection); // 0 = Ok  |  1 = Error
        cb(return_value);
      }else {
        console.log('Table "produit" exist.\n');
        // console.log(rows);
        return_value = 0;
        cb(return_value);
      }
    });
  }
}

you should really read few "intro to async" articles first, here is list I googled for you:

https://www.codementor.io/nodejs/tutorial/manage-async-nodejs-callback-example-code
http://justinklemm.com/node-js-async-tutorial/
http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
http://book.mixu.net/node/ch7.html

All 2 comments

same approach, again - you "return" values by passing them to callback function as a parameter:

module.exports.isTableExist = function(connection, cb) {
    connection.query("SELECT * FROM product;", function(err, rows, fields) {
      if (err) {
        console.log('Table doesn\'t exist.\n');
        return_value = createTable(connection); // 0 = Ok  |  1 = Error
        cb(return_value);
      }else {
        console.log('Table "produit" exist.\n');
        // console.log(rows);
        return_value = 0;
        cb(return_value);
      }
    });
  }
}

you should really read few "intro to async" articles first, here is list I googled for you:

https://www.codementor.io/nodejs/tutorial/manage-async-nodejs-callback-example-code
http://justinklemm.com/node-js-async-tutorial/
http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
http://book.mixu.net/node/ch7.html

Thank you so much for your time, I will read that very carefully !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

winzig picture winzig  路  4Comments

whatthehell232 picture whatthehell232  路  3Comments

skilbjo picture skilbjo  路  3Comments

DmitryEfimenko picture DmitryEfimenko  路  4Comments

EdoardoPedrotti picture EdoardoPedrotti  路  3Comments