Let's say you promisify a MySQL connection object (in order to make raw MySQL queries):
var con = Promise.promisify(MySQL.newCon());
And then, you start a promise chain with some ORM method. Inside of the first then
, you get a color
object, returned from the Color
model, but then, you want to start a MySQL transaction AND pass that color to the next then
in the promise chain (but also ensure that the transaction starts):
Color
.findOne() // ORM looks for a color within the model (table)
.then(function (color) {
// A transaction is started
// Whatever this query returns is useless,
// Since it starts a transaction.
// I'd like to actually pass the color to the next `then`.
return con.query('START TRANSACTION');
})
.then(function (color) {
console.log(color);
});
I'm thinking that returning con.query
ensures that the transaction is started (and the promise chain continues), but is that the only way? I know I could do:
return new Promise(function (resolve, reject) {
con.query('START TRANSACTION', function (err) {
if (err) {
return reject(new Error(err));
}
resolve(color); // Pass the color to the next then
});
});
Or could also return an array and use spread
instead of then
:
return [color, con.query('START TRANSACTION')];
Are these the only ways of doing this? Ideally, I'd like something that is very similar to the first example I posted, because I think that returning a new Promise
looks dirty, and returning an array is kind of confusing, since I don't care about what con.query
returns at all.
Thanks.