extending Q Promise objects?

238 views
Skip to first unread message

Wout Mertens

unread,
Jul 12, 2013, 7:08:26 AM7/12/13
to q-con...@googlegroups.com
I'd like to decorate my promises with a dump() method, for helping with debugging.

This naive approach doesn't work:

Q().prototype.dump = function() {
  return this["finally"](function(o) {
    return dump(o);
  });
};

(CoffeeScript: Q().prototype.dump = -> @finally (o) -> dump o)

How should I do this instead?

Wout.

Domenic Denicola

unread,
Jul 12, 2013, 9:25:02 AM7/12/13
to q-con...@googlegroups.com

Remember that Q() is a single promise instance, and in general in JavaScript, instances do not have meaningful `prototype` properties.

 

You should do Q.Promise.prototype.dump = …, or if you’re really keen on modifying the prototype via an existing Q instance, Object.getPrototypeOf(Q()).dump = ….

--
You received this message because you are subscribed to the Google Groups "Q Continuum (JavaScript)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to q-continuum...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Domenic Denicola

unread,
Jul 12, 2013, 9:28:52 AM7/12/13
to q-con...@googlegroups.com

I forgot that Q.Promise might not work in the released versions of Q; I think it’s only been exposed in master. In that case the Object.getPrototypeOf route is your best bet.

Reply all
Reply to author
Forward
0 new messages