CodeFoodPixels / node-promise-mysql

A wrapper for mysqljs/mysql that wraps function calls with Bluebird promises.
MIT License
338 stars 63 forks source link

outer promise on update resolves first then inner #19

Closed GGeorgi closed 8 years ago

GGeorgi commented 8 years ago

Hi, I have got a strange problem. Everything works good but on update queries promise1 has being resolved first then inner one. Any ideas ?

var  callString = "update table1 set name = 'aaa' where id = 1";
var promise1 = return connection.query(callString).then(function(ok){
         var promise2 = q.resolve("done");
         return promise2;
});

Thanks.

CodeFoodPixels commented 8 years ago

Your example code is full of errors, so I can't try and replicate it.

Edit: Ok, so I'm guessing you're trying to use the Q promises library as the one inside the mysql call?

GGeorgi commented 8 years ago

:) it is completely clear that this code will not work. It is just an example to make you better understand what I mean. Anyway if you want more complete code here it is.

import mysql = require('promise-mysql');
import Q = require("q");

class ServiceBase {
    public static pool = mysql.createPool({
        connectionLimit: 500,
        host: 'ipaddress',
        user: 'username',
        password: 'password',
        database: 'scheme',
        multipleStatements: true
    });

    public static Query(connection:any, callString:string) {
        return connection.query(callString).then(function (rows) {
            return Q.resolve(rows);
        }).catch(function (err) {
            return ServiceBase.RollBackConnection(connection, err);
        });
    }

...
...

    protected static CreateConnection():any {
        console.log("create connection");
        return this.pool.getConnection().then(function (connection) {
            return Q.resolve(connection)
        }).catch(function (err) {
            return Q.reject(err);
        });
    }

...
...
}
export = ServiceBase;

so if you will call ServieBase.Query with some update query then promise which has returned by connection.queryresolves more faster then inner one: Q.resolve(rows); And that happens only on update query, on select it is ok.

Thanks.

EDIT: update query looks like this call message_update_message('78',3,'sd','Go ahead…sd',2)

CodeFoodPixels commented 8 years ago

Based on both of your examples, I hacked this together

var mysql = require('promise-mysql');
var q = require('q');

mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'database'
}).then(function(connection){
    var  callString = "update table1 set name = '123' where id = 1";
    var promise1 = connection.query(callString).then(function(ok){
         var promise2 = q.resolve("promise 2");
         return promise2;
    }).then(function(prev) {
        console.log(prev)
        console.log('promise 1')
    })
});

When running this I get the following in the terminal:

promise 2
promise 1

Which is what I'd expect.

GGeorgi commented 8 years ago

Thank you. My fall :)