It would be nice if functions which take a callback (connect, query, etc.) would return a promise/thenable.
The API would have to change because thenable callbacks only receive a single parameter and errors are sent through reject.
This would allow the library to work seemless with tools like co and prepare it for async/await which is coming in ES7.
:+1:
:+1:
:+1:
If connection and query methods returned a promise, this is how you would write a query:
"use strict";
var pg = require('pg');
var conString = process.env.DATABASE_URL;
// Declare query with generators + promises
function* getQueryResult(query){
var client = yield pg.connect(conString);
var result = yield client.query(query);
return result
}
// helper function
var spawn = function(generator) {
return new Promise((accept,reject)=>{
let onResult = (lastPromiseResult)=>{
var {value,done} = generator.next(lastPromiseResult);
done ? accept(value) : value.then(onResult,reject);
}
onResult();
})
};
// Run your query
spawn(getQueryResult('SELECT $1::int AS number', ['1']))
.then(result => console.log(result.rows[0].number);)
.catch(err => console.log(err))
:+1:
This:
Promise.all([firstQuery, secondQuery]).then(function(){
});
would make a lot of things more readable.
@brianc, comments?
For the time being I think this should probably be accomplished via an add-on module rather than adding even more functionality into node-postgres itself. This module is already a bit bloated (connection pooling, for example) and I am not really keen on adding more things and breaking API backwards compatibility in such a substantial way when it could be accomplished via another lib like this for example. Then the API could be fleshed out there, agreed upon, pounded on with production usage. When promises actually land officially in node in some way - either through ES7 or node core - then we could revisit modifying the query object to be a promise.
Bump - promises were added to node in v0.11 I believe, and would certainly be in io.js (all versions). Additionally, es6 is now finished and published with promises.
I agree. Promises are absolutely amzing with async/await too. Ill have to
take a look if the API can be done cleanly here without breaking backward
compat. I wanna decompose this module into a few and can probably hit it
then.
On Saturday, August 15, 2015, Thomas Foster [email protected]
wrote:
Bump - promises were added to node in v0.11 I believe, and would certainly
be in io.js (all versions). Additionally, es6 is now finished and published
with promises.—
Reply to this email directly or view it on GitHub
https://github.com/brianc/node-postgres/issues/694#issuecomment-131398728
.
+1
@brianc - regarding API, the de facto standard seems to be to return a Promise if no callback is provided. This wouldn't work always in the case of node-postgres, because for example, client.query() returns a Query object.
I think a good solution would be to have a promise available as a property on Query - so something like client.query("SELECT * FROM example;").result would be possible. Doesn't break backwards compatibility, though might add a little code complexity.
@thomasfoster96 Wouldn't it be possible to extend the returned Query object with the Promise interface, i.e. a then() and a catch() method?
Or the other way around: client.query() could return a Promise with an additional on() function for the existing events.
Here is a Promise-based wrapper library for node-postgres library designed for easy use with ES7 async/await: https://github.com/kriasoft/node-pg-client
Here is another, mine: pg-async :elephant:
I hope I won't sound arrogant to suggest that pg-promise concludes this discussion nicely, supporting all promise libraries, with high-level transactions and lots more...
@brianc, @EliSnow ... and that it is ok to close the issue ;)
async/await is coming to v8 very soon. The pg module should be prepared.
Adding support for promises is not feature bloat considering promises are by now a language primitive. Furthermore, adding promise support would not be a breaking change as callbacks could exist simultaneously.
@brianc, this should probably be closed with pg 6.0 out with pg-pool. Right?
(Right!)
Most helpful comment
async/await is coming to v8 very soon. The
pgmodule should be prepared.Adding support for promises is not feature bloat considering promises are by now a language primitive. Furthermore, adding promise support would not be a breaking change as callbacks could exist simultaneously.