Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-2376

nodejs: allow Promise style calls for client and server

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 0.9.2
    • 0.9.2
    • Node.js - Compiler
    • None

    Description

      the idea of this patch is to allow writing server functions and clients calls using future/promise style.

      for instance a server function:

      {
        divide: function(n1, n2, result) {
          if (n2 === 0) {
            var x = new ttypes.InvalidOperation();
            result(x);
            return;
          }
          result(null, n1/n2);
        }
      }
      

      might be written as:

      {
        divide: function(n1, n2) {
          if (n2 === 0) {
            throw new ttypes.InvalidOperation();
          }
          return n1/n2;
        }
      }
      

      both style remains valid, the style to use is detected according to the function signature (is there a last argument for the callback).

      when using promise style, a promise can also be return in stead of the result for asynchronous results.

      the client side might use promise as well:

      client.calculate(1, work, function(err, message) {
        if (err) {
          console.log("InvalidOperation " + err);
        } else {
          console.log('Whoa? You know how to divide by zero?');
        }
      });
      

      might be written as:

      client.calculate(1, work)
        .then(function(message) {
              console.log('Whoa? You know how to divide by zero?');
        })
        .fail(function(err) {
          console.log("InvalidOperation " + err);
        });
      

      Attachments

        Activity

          People

            henrique Henrique Mendonca
            chubinou Pierre Lamot
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: