Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise #66

Open
eyy opened this issue Jan 8, 2015 · 4 comments
Open

Promise #66

eyy opened this issue Jan 8, 2015 · 4 comments

Comments

@eyy
Copy link

eyy commented Jan 8, 2015

Are there any plans for Promise support, alongside the callbacks and streams?

Thanks.

@grncdr
Copy link
Owner

grncdr commented Jan 10, 2015

I have thought about it, but haven't got around to implementing anything yet. A few months ago I was thinking of adding 3 promise-returning methods on Query instances:

  • .reduce( initialValue, (accumulator, row) => accumulator ) - takes an initial value and a reducer function. Returns a promise for the reduced value.
  • .then( (result) => any, (error) => any ) - Same mechanics as providing a callback: all data is aggregated into a result which is passed to onSuccess, or an error is passed to onError.
  • .map( (row) => any ) - Takes a function to map over result rows and returns a promise for a new Result object.

These would not be terrible difficult to implement on top of the existing stream API in a separate package that each adapter could re-use. Something like any-db-query-extensions that mixes the methods into a given object/prototype. If you're interested in writing that lib I would use it in the adapters I maintain.

@Meaglin
Copy link

Meaglin commented Jun 15, 2015

Proper Promise support in any-db and any-db-transaction would be really nice :)

@vitaly-t
Copy link

@Meaglin, check out this one: pg-promise.

Proper Promise support in any-db and any-db-transaction would be really nice :)

@Justus-Maier
Copy link

To make use of the async/await syntax we need promise returns.
Workaround using promise-streams:

const wait = require('promise-streams').wait;
[...]
  await wait(connection.query(sql).on('data', row => console.log(row)));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants