You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to suggest that the JS driver use the Promises/A+ spec. Callbacks are very important to support because they are the de-facto style, but that doesn't mean that promises should be unsupported. Modules can return a promise and still call a callback, allowing both to work. If you're on the fence with regards to promises, just consider that the following 2 examples do the same thing:
I'm not a big fan of promises for different reasons, but some people do like them, so we will probably add them at some point. It's not on the immediate roadmap though.
I would tend to think that it's not too hard -- but I'm not familiar enough with them to properly judge how much work it would be.
I'll try to take a look at how doable it is next time I'll have some free time.
fyi, gen-run provides a way to use the traditional callback-style functions as well. So I guess the following should already work (notice the 'resume' callback; when called, its parameters will be returned from the yield):
Good point, I didn't know that. Thanks for the pointer! Btw, your code actually doesn't completely work because you have to call the resume function. But the following code works:
var run = require('gen-run');
run(function* (resume) {
var conn = yield r.connect({ host: 'localhost', port: 28015 }, resume());
console.log(yield r.db('test').tableCreate('tv_shows').run(conn, resume()));
console.log(yield r.table('tv_shows').insert({ name: 'Star Trek TNG' }).run(conn, resume()));
});
It's almost complete. I couldn't run completly run RethinkDB's polyglot mostly because backtraces are not exactly the same, but I believe all the terms/options are defined.
I'll keep adding tests, but hopefully it should be out soon.
@pilwon We have to wait for Node 0.12 to be released first.
Then I'm not quite sure what's going to happen.
I believe promises/generators will take over the callback, but that's going to take some time (hopefully not too much).
So we are probably going to keep the two drivers for some time. That would also give rethinkdbdash some time to be tested in the wild.
Then when promises will be the normal way to write code with Node.js, we'll tag rethinkdbdash as our official Node.js driver.
Writing the driver was some work, but updating it with new terms is really easy to do, so I'll keep it up to date, no need to worry about that :)
@neumino I agree that the addition of generator could wait until Node.js 0.12, but I'd hope that promise be added sooner. In my opinion, promise is just another style and callback style will probably never be completely replaced with callback. Instead of waiting until the perfect signal (which I don't think will happen), one way would be to add support for both, like some other projects do, and give users an option.
I usually have no problem wrapping any callback-only code with q's Q.denodeify or Q.nbind but rethinkdb driver makes it really tricky to wrap it because the query builder functions are all built into the same module. I also tried rql-promise but what I concluded was it's the cleanest and the easiest to just add this to the official driver, especially when it doesn't take too much effort as you mentioned.
It's currently one of the biggest pain points for me right now as user's perspective. (mixing with other promise-based code)
@coffeemugrql-promise feels more like a hack to me. The use of rql() and rql.lazy() quickly turns chained query bulky and less readable, especially for slightly more complex queries. I'd like to use only the officially supported client library as well, if possible.
Here are some examples supporting both callback and promise styles:
I think it would be cool if there was an r.connectPromise which would act like r.connect but it would return a promise. One could then wait until that promise is fulfilled with either .then() (Q-style) or by using a generator that yeilds to the promise.
Queries could be run with just .run(conn) if conn is a connection that was made with r.connectPromise and the way that one could tell if it was made with r.connectPromise is the connection object could have a property isPromise that could be set to true if the user wants to use promises or false if the user wants to use callbacks.
The second. Although I don't plan to do that, I actually plan to use gen-run. I would write an example but my math teacher is still teaching (ugh stupid algebra 2), and if I type too much he might notice it.
varrun=require('gen-run')run(function*(){varconn=yeildr.connectPromise()// run queries like:yeildr.table('foo').run(comm)// another example, taken from the rethinkdb website:yeildr.table('FairVerona').insert(r.table('Montagues').union(r.table('Capulets'))).run(con)},function(err,value){if(err)throwerrelseconsole.log(value)})
@goatic I don't think the promise variants are documented, but for (almost) any method that takes a callback, the callback is optional and a promise is returned.
@goatic Note that all ReQL terms such as dbDrop, dbList etc. are non-blocking functions in JavaScript that only build up a "query object". The command that actually executes the query and that is blocking is run, which has both a promise- and a callback-style interface (which is documented). The examples in the individual commands are often using the callback-style I think.
Activity
neumino commentedon Aug 30, 2013
I haven't try it myself, but you may be interested in
https://github.com/guillaumervls/rql-promise
I'm not a big fan of promises for different reasons, but some people do like them, so we will probably add them at some point. It's not on the immediate roadmap though.
I would tend to think that it's not too hard -- but I'm not familiar enough with them to properly judge how much work it would be.
I'll try to take a look at how doable it is next time I'll have some free time.
coffeemug commentedon Aug 30, 2013
Moving to backlog for now, but I think it's a cool idea. If promises are wildly adopted, count us in!
pixelspark commentedon Sep 2, 2013
fyi, gen-run provides a way to use the traditional callback-style functions as well. So I guess the following should already work (notice the 'resume' callback; when called, its parameters will be returned from the yield):
jamescostian commentedon Sep 2, 2013
Good point, I didn't know that. Thanks for the pointer! Btw, your code actually doesn't completely work because you have to call the resume function. But the following code works:
var run = require('gen-run');
run(function* (resume) {
var conn = yield r.connect({ host: 'localhost', port: 28015 }, resume());
console.log(yield r.db('test').tableCreate('tv_shows').run(conn, resume()));
console.log(yield r.table('tv_shows').insert({ name: 'Star Trek TNG' }).run(conn, resume()));
});
neumino commentedon Jan 21, 2014
If someone is following this thread, there is a new driver that uses promises.
https://github.com/neumino/rethinkdbdash
It's almost complete. I couldn't run completly run RethinkDB's polyglot mostly because backtraces are not exactly the same, but I believe all the terms/options are defined.
I'll keep adding tests, but hopefully it should be out soon.
coffeemug commentedon Jan 21, 2014
👍
pilwon commentedon Jan 31, 2014
👍 👍 👍
@neumino Will we see your project merged with the official Node.js client?
neumino commentedon Jan 31, 2014
@pilwon We have to wait for Node 0.12 to be released first.
Then I'm not quite sure what's going to happen.
I believe promises/generators will take over the callback, but that's going to take some time (hopefully not too much).
So we are probably going to keep the two drivers for some time. That would also give rethinkdbdash some time to be tested in the wild.
Then when promises will be the normal way to write code with Node.js, we'll tag rethinkdbdash as our official Node.js driver.
Writing the driver was some work, but updating it with new terms is really easy to do, so I'll keep it up to date, no need to worry about that :)
pilwon commentedon Jan 31, 2014
@neumino I agree that the addition of generator could wait until Node.js 0.12, but I'd hope that promise be added sooner. In my opinion, promise is just another style and callback style will probably never be completely replaced with callback. Instead of waiting until the perfect signal (which I don't think will happen), one way would be to add support for both, like some other projects do, and give users an option.
I usually have no problem wrapping any callback-only code with q's
Q.denodeifyorQ.nbindbut rethinkdb driver makes it really tricky to wrap it because the query builder functions are all built into the same module. I also tried rql-promise but what I concluded was it's the cleanest and the easiest to just add this to the official driver, especially when it doesn't take too much effort as you mentioned.It's currently one of the biggest pain points for me right now as user's perspective. (mixing with other promise-based code)
coffeemug commentedon Jan 31, 2014
@pilwon -- I have a couple of questions about this.
Could you give some more details about why that is? Why would it be cleaner/better than using rql-promise (or one of the other wrappers like it)?
Could you give an example of a project or two that offer both and do it well? Mixing the two seems hard to do cleanly.
pilwon commentedon Jan 31, 2014
@coffeemug
rql-promisefeels more like a hack to me. The use ofrql()andrql.lazy()quickly turns chained query bulky and less readable, especially for slightly more complex queries. I'd like to use only the officially supported client library as well, if possible.Here are some examples supporting both callback and promise styles:
jamescostian commentedon Jan 31, 2014
I think it would be cool if there was an
r.connectPromisewhich would act liker.connectbut it would return a promise. One could then wait until that promise is fulfilled with either.then()(Q-style) or by using a generator that yeilds to the promise.Queries could be run with just
.run(conn)if conn is a connection that was made withr.connectPromiseand the way that one could tell if it was made withr.connectPromiseis the connection object could have a propertyisPromisethat could be set to true if the user wants to use promises or false if the user wants to use callbacks.neumino commentedon Jan 31, 2014
Do you expect a syntax like that
Or like that
Or both?
jamescostian commentedon Jan 31, 2014
The second. Although I don't plan to do that, I actually plan to use gen-run. I would write an example but my math teacher is still teaching (ugh stupid algebra 2), and if I type too much he might notice it.
On Fri, Jan 31, 2014 at 11:05 AM, Michel [email protected] wrote:
jamescostian commentedon Jan 31, 2014
Ok I'm in lunch. So here's what I would do:
56 remaining items
neumino commentedon Apr 23, 2014
@JamesS237 -- Yup, it's backward compatible as far as I know.
neumino commentedon May 19, 2014
Merged in next as 6969527
Ship 1.13
jamescostian commentedon May 19, 2014
Awesome!
StreetStrider commentedon May 19, 2014
Great 👍
Are promises reflected in documentation?
neumino commentedon May 19, 2014
The API docs are in this commit
rethinkdb/docs@d14e8ea
It hasn't been merged in
masterthough.Basically every method that takes a callback will return a promise if no callback is provided. The only exception is
cursor.each.artyomxx commentedon Feb 8, 2016
How about promises with
update()(and probably all otherwritingfunctions)?deontologician commentedon Feb 8, 2016
@lolWhoAmI this is a pretty old issue. Would you mind opening a new issue if you have a feature request?
marshall007 commentedon Apr 13, 2016
@goatic I don't think the promise variants are documented, but for (almost) any method that takes a callback, the callback is optional and a promise is returned.
danielmewes commentedon Apr 19, 2016
@goatic Note that all ReQL terms such as
dbDrop,dbListetc. are non-blocking functions in JavaScript that only build up a "query object". The command that actually executes the query and that is blocking isrun, which has both a promise- and a callback-style interface (which is documented). The examples in the individual commands are often using the callback-style I think.