Skip to content

Promises/A+ for JS driver #1395

Closed
Closed

Description

@jamescostian

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:

r.connect({ host: 'localhost', port: 28015 }, function(err, conn) {
  if(err) throw err;
  r.db('test').tableCreate('tv_shows').run(conn, function(err, res) {
    if(err) throw err;
    console.log(res);
    r.table('tv_shows').insert({ name: 'Star Trek TNG' }).run(conn, function(err, res)
    {
      if(err) throw err;
      console.log(res);
    });
  });
});

NB: the following uses gen-run and JS ES6

var run = require('gen-run');
run(function* () {
  var conn = yield r.connect({ host: 'localhost', port: 28015 });
  console.log(yield r.db('test').tableCreate('tv_shows').run(conn));
  console.log(yield r.table('tv_shows').insert({ name: 'Star Trek TNG' }).run(conn));
});

Activity

  1. neumino commented on Aug 30, 2013

    @neumino
    Member

    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.

  2. coffeemug commented on Aug 30, 2013

    @coffeemug
    Contributor

    Moving to backlog for now, but I think it's a cool idea. If promises are wildly adopted, count us in!

  3. pixelspark commented on Sep 2, 2013

    @pixelspark

    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):

    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));
    });
  4. jamescostian commented on Sep 2, 2013

    @jamescostian
    Author

    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()));
    });

  5. neumino commented on Jan 21, 2014

    @neumino
    Member

    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.

  6. coffeemug commented on Jan 21, 2014

    @coffeemug
    Contributor

    👍

  7. pilwon commented on Jan 31, 2014

    @pilwon

    👍 👍 👍

    @neumino Will we see your project merged with the official Node.js client?

  8. neumino commented on Jan 31, 2014

    @neumino
    Member

    @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 :)

  9. pilwon commented on Jan 31, 2014

    @pilwon

    @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)

  10. coffeemug commented on Jan 31, 2014

    @coffeemug
    Contributor

    @pilwon -- I have a couple of questions about this.

    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

    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)?

    one way would be to add support for both, like some other projects do, and give users an option.

    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.

  11. pilwon commented on Jan 31, 2014

    @pilwon
  12. jamescostian commented on Jan 31, 2014

    @jamescostian
    Author

    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.

  13. neumino commented on Jan 31, 2014

    @neumino
    Member

    Do you expect a syntax like that

    var connection = r.connectPromise();
    r.table("foo").run(connection).then(function(result) {
        //...
    }).error(function(error) {
        //...
    })

    Or like that

    r.connectPromise().then(function(connection) {
        r.table("foo").run(connection).then(function(result) {
            //...
        }).error(function(error) {
            //...
        })
    }).error(function(error) {
        //...
    })

    Or both?

  14. jamescostian commented on Jan 31, 2014

    @jamescostian
    Author

    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:

    Do you expect a syntax like that

    var connection = r.connectPromise();
    r.table("foo").run(connection).then(function(result) {
        //...
    }).error(function(error) {
        //...
    })

    Or like that

    r.connectPromise().then(function(connection) {
        r.table("foo").run(connection).then(function(result) {
            //...
        }).error(function(error) {
            //...
        })
    }).error(function(error) {
        //...
    })

    Or both?

    Reply to this email directly or view it on GitHub:
    #1395 (comment)

  15. jamescostian commented on Jan 31, 2014

    @jamescostian
    Author

    Ok I'm in lunch. So here's what I would do:

    var run = require('gen-run')
    run(function* () {
        var conn = yeild r.connectPromise()
        // run queries like:
        yeild r.table('foo').run(comm)
        // another example, taken from the rethinkdb website:
        yeild r.table('FairVerona').insert(r.table('Montagues').union(r.table('Capulets'))).run(con)
    }, function (err, value) {
        if (err) throw err
        else console.log(value)
    })
  16. 56 remaining items

  17. neumino commented on Apr 23, 2014

    @neumino
    Member

    @JamesS237 -- Yup, it's backward compatible as far as I know.

  18. neumino commented on May 19, 2014

    @neumino
    Member

    Merged in next as 6969527
    Ship 1.13

  19. modified the milestones: 1.13, on May 19, 2014
  20. jamescostian commented on May 19, 2014

    @jamescostian
    Author

    Awesome!

  21. StreetStrider commented on May 19, 2014

    @StreetStrider

    Great 👍
    Are promises reflected in documentation?

  22. neumino commented on May 19, 2014

    @neumino
    Member

    The API docs are in this commit
    rethinkdb/docs@d14e8ea

    It hasn't been merged in master though.

    Basically every method that takes a callback will return a promise if no callback is provided. The only exception is cursor.each.

  23. artyomxx commented on Feb 8, 2016

    @artyomxx

    How about promises with update() (and probably all other writing functions)?

  24. deontologician commented on Feb 8, 2016

    @deontologician
    Contributor

    @lolWhoAmI this is a pretty old issue. Would you mind opening a new issue if you have a feature request?

  25. marshall007 commented on Apr 13, 2016

    @marshall007
    Contributor

    @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.

  26. danielmewes commented on Apr 19, 2016

    @danielmewes
    Member

    @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.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions