Using deferrables to perform operations on Redis

22 views
Skip to first unread message

Patrick Mulder

unread,
Oct 11, 2013, 11:08:40 AM10/11/13
to jsclas...@googlegroups.com
Hi!

I want to understand how to use the Deferrable module from JSClass.

The context where I am working is an action to create a User in a Redis hash:

1. Increase counter to get ID
2. Set Redis hash of user with ID
3. Return created user from Redis

I now have:

  var incrId = function() {
    var promise = new Promise();
    client.hincrby('users', 'count', 1, function(err, id){
      promise.succeed(addUser); 
    });
    return promise;
  };

  var addUser = function() {
     // never gets called
  };

  incrId();  // main call

Any ideas why the promise doesn't call addUser?

Thanks!

Patrick Mulder

unread,
Oct 11, 2013, 11:09:33 AM10/11/13
to jsclas...@googlegroups.com
Additionally Promise is defined with:

var Class = require('jsclass/src/core').Class;
var Deferrable = require('jsclass/src/deferrable').Deferrable;
var Promise = new Class({
  include: Deferrable,
  
  initialize: function(value) {
    // if value is already known, succeed immediately
    if (value !== undefined) this.succeed(value);
  }
});

James Coglan

unread,
Oct 12, 2013, 8:42:55 AM10/12/13
to jsclas...@googlegroups.com
On 11 October 2013 16:08, Patrick Mulder <mulder....@gmail.com> wrote:
The context where I am working is an action to create a User in a Redis hash:

1. Increase counter to get ID
2. Set Redis hash of user with ID
3. Return created user from Redis

I now have:

  var incrId = function() {
    var promise = new Promise();
    client.hincrby('users', 'count', 1, function(err, id){
      promise.succeed(addUser); 
    });
    return promise;
  };

  var addUser = function() {
     // never gets called
  };

  incrId();  // main call

Any ideas why the promise doesn't call addUser?

Deferrable#succeed takes a *value* and passes it to callbacks registered with callback() what you probably want is this:

  var incrId = function() {
    var promise = new Promise();
    client.hincrby('users', 'count', 1, function(err, id){
      promise.succeed(id); 
    });
    return promise;
  };

  var addUser = function(id) {
     // never gets called
  };

  incrId().callback(addUser);

However these days I wouldn't recommend using Deferrable, it's an old thing that predates the standardized Promise interface (http://promisesaplus.com/). Find an implementation of that (some npm modules: q, rsvp, promise) instead.

Patrick Mulder

unread,
Oct 12, 2013, 5:28:13 PM10/12/13
to jsclas...@googlegroups.com


On Saturday, October 12, 2013 2:42:55 PM UTC+2, James Coglan wrote:

However these days I wouldn't recommend using Deferrable, it's an old thing that predates the standardized Promise interface (http://promisesaplus.com/). Find an implementation of that (some npm modules: q, rsvp, promise) instead.

Thanks, it is definitively a jungle of npm modules out there addressing this.

I like bluebird as discussed here:

Reply all
Reply to author
Forward
0 new messages