Skip to content

Why don't we use promises in our API? #551

Description

@jgeewax

UPDATE (4/25/16)

Promises coming soon. Track progress here: #1142


Seems like we're passing callbacks in rather than getting promises back -- was this an explicit choice?

/cc @idosela

Activity

  1. added
    type: questionRequest for information or clarification. Not an issue.
    on May 8, 2015
  2. added this to the Core Future milestone on May 8, 2015
  3. stephenplusplus commented on May 8, 2015

    @stephenplusplus
    Contributor

    Why should we use promises? We talked about it before, but Node is callbacks, promises are an abstraction layer that not everybody enjoys using.

  4. jgeewax commented on May 8, 2015

    @jgeewax
    ContributorAuthor

    I'll leave it to @idosela to chime in on why he thinks they're great :)

    I actually think they're nice, but don't think it's a make-or-break.

  5. ryanseys commented on May 8, 2015

    @ryanseys
    Contributor

    Yeah I think it'd be way too much work now (for us and for our users) to switch. That's a design decision definitely made up front. Also, all our dependencies and the large majority of npm packages in general use callbacks, so while I can understand the appeal for Promises for certain cases, they don't exactly sit well with what people come to expect from a Node library.

  6. jgeewax commented on May 8, 2015

    @jgeewax
    ContributorAuthor

    And I'm guessing that if someone wanted to, they could wrap what we have and add promises... I'm going to close this out as I think we've answered the question:

    • Yes, we talked about it before, but it wasn't a consistent with the rest of the Node libraries out there.
    • Yes, it's nice, but we probably won't add it.
    • If someone really loves promises, they're welcome to fork and maintain their own wrapper?

    Feel free to re-open @idosela if you think we're making a huge mistake, otherwise we'll continue with our trusty ol' callbacks :)

  7. jgeewax commented on May 8, 2015

    @jgeewax
    ContributorAuthor
  8. idosela commented on May 10, 2015

    @idosela

    TL;DR; I like promises, but I understand it would take a lot of work to switch to promises, so feel free to leave the issue closed.

    I find promises to be very convenient for handling async operations. The most common use cases in which promises provide significant advantage over callbacks are combination, chaining, and error handling.

    Let's say I want to fire off 10 Datastore queries, and then do something when they are all done.
    With callbacks I would have to write a bunch of code to track when each query is complete. With promises I can combine all of the promises returned by each individual query into a single promise which will be resolved when all of the queries are done. All it takes is a single function call.

    Promise chaining allows you to break down sequential async operations which depend on each other's return values, without having to resort to nesting of callbacks. This leads to a more readable and testable code.

    Promises also allow the user of your API to write less code, and have more flexibility. For example, with callbacks I constantly have to check whether an error happened, and can't have a single error handler for multiple related operations.

    Callbacks

    dataset.save({key: blogPostKey, data: blogPostData}, function(err, response) {
      // Handle error in step 1.
      if (err) {
        return;
      }
    
      dataset.save({key: blogPostKey, data: {isDraft: false}}, function(err, response) {
        // Handle error in step 1.
        if (err) {
          return;
        }
    
        // Do something on success.
      });  
    });

    Promises

    dataset.save({key: blogPostKey, data: blogPostData})
        .then(function(response) {
          return dataset.save({key: blogPostKey, data: {isDraft: false}});
        })
        .then(function(response) {
          // Do something on success.
        })
        .catch(function(err) {
          // Handle errors from step 1 or 2.
        });

    Q (https://github.com/kriskowal/q) provides much more detailed comparison of callbacks and promises, and I know of several Node packages that use it. I'm no Node expert, but it seems to me Promises are a pattern that is not language or environment specific.

    Sorry for going on and on :) Have a great weekend!

  9. jgeewax commented on May 10, 2015

    @jgeewax
    ContributorAuthor

    Out of curiosity -- is there a way we could easily support both ?

  10. jgeewax commented on Jun 6, 2015

    @jgeewax
    ContributorAuthor

    I'm reopening this issue mainly because ... I've been writing more stuff in Node and promises are becoming nicer and nicer.

    Some libraries that use promises that seem really nice:

    I think our library would be... kind of amazing if we could offer promises as well as the callback style we have today.

    If we were to invest in this, I'd love if it we could do the work to support promises, without documenting them, and once they are ubiquitous in the library, we post the documentation.

    Thoughts?

  11. reopened this on Jun 6, 2015
  12. ryanseys commented on Jun 8, 2015

    @ryanseys
    Contributor

    What value does it add? What do you mean by "nicer and nicer"? Callbacks vs. Promises is mainly, I feel, just a personal preference. I don't like the idea of mixing both callbacks and promises because it can confuse people if they mix them. Do you know of any popular node libraries that support both callbacks and promises out of the box?

  13. jgeewax commented on Jun 8, 2015

    @jgeewax
    ContributorAuthor

    What value does it add? What do you mean by "nicer and nicer"? Callbacks vs. Promises is mainly, I feel, just a personal preference.

    I'd consider the nesting of callbacks to be an anti-pattern... Further, "we accept a callback" is rather restrictive -- and "we return a promise, do what you want" is much more open.

    I don't like the idea of mixing both callbacks and promises because it can confuse people if they mix them.

    This could be true, but what's wrong with saying in the docs: "this method accepts a callback, and returns a promise" ? Does that really confuse you?

    Do you know of any popular node libraries that support both callbacks and promises out of the box?

    @idosela : Do you know of any popular node libraries that support both? I don't know of any off the top of my head...

  14. 96 remaining items

  15. added a commit that references this issue on Feb 2, 2026
  16. added a commit that references this issue on Feb 3, 2026
  17. added a commit that references this issue on Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions