I'm running Node 7.5.0 with the --harmony flag, which enables support for async/await. I'm using the client pool with async/await, which works great and has a nice example here.
The example for transactions (here) uses callbacks instead of async/await, so I thought I'd try something like this as a quick test:
let client = null;
try {
client = await this.pool.connect();
} catch (error) {
console.log('A client pool error occurred:', error);
return error;
}
try {
await client.query('BEGIN');
await client.query('UPDATE foo SET bar = 1');
await client.query('UPDATE bar SET foo = 2');
await client.query('COMMIT');
} catch (error) {
try {
await client.query('ROLLBACK');
} catch (rollbackError) {
console.log('A rollback error occurred:', rollbackError);
}
console.log('An error occurred:', error);
return error;
} finally {
client.release();
}
return 'Success!';
This seems to work just fine, but is this a bad idea? Is there some technical or other reason I should be using the callback approach with transactions?
I'm running Node 7.5.0 with the
--harmonyflag, which enables support for async/await. I'm using the client pool with async/await, which works great and has a nice example here.The example for transactions (here) uses callbacks instead of async/await, so I thought I'd try something like this as a quick test:
This seems to work just fine, but is this a bad idea? Is there some technical or other reason I should be using the callback approach with transactions?