<HTML>
<HEAD>
<TITLE>Twisted vs jQuery Deferreds (was Re: [Twisted-Python] RPC design questions)</TITLE>
</HEAD>
<BODY>
<FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>I follow Glyphs advice and only keep the lambda version with standard Twisted Deferred.<BR>
<BR>
However, I've got a (I believe) more serious &quot;design&quot; problem.<BR>
<BR>
The RPC stuff works in Twisted, but also from JavaScript.<BR>
<BR>
So I have 2 audiences (Twisted and JS developers).<BR>
<BR>
On JS, I use the Deferreds that come standard with jQuery since 1.5.<BR>
<BR>
Now compare the following Twisted code (self.call does the RPC and returns a Twisted Deferred):<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# prints 23<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d2 = self.call(&quot;square&quot;, 23).addCallback(lambda res: \<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.call(&quot;sqrt&quot;, res)).addCallback(self.show)<BR>
<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# prints 23<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d3 = self.call(&quot;square&quot;, 23).addCallback(lambda res: \<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.call(&quot;sqrt&quot;, res).addCallback(self.show))<BR>
<BR>
(note the subtle difference in bracketing)<BR>
<BR>
with the following JS<BR>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// prints 529<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sess.call(&quot;square&quot;, 23).then(function(res) {<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return sess.call(&quot;sqrt&quot;, res);<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}).then(console.log);<BR>
<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// prints 23<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sess.call(&quot;square&quot;, 23).then(function(res) {<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sess.call(&quot;sqrt&quot;, res).then(console.log);<BR>
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<BR>
<BR>
===<BR>
<BR>
The JS is structurally/syntactically similar to the Py version.<BR>
<BR>
However they behave differently.<BR>
<BR>
There seems to be a fundamental difference between Twisted and jQuery Deferreds.<BR>
<BR>
When calling then() on a jQuery Deferred D, it seems to return D, not any Deferred that might<BR>
be returned within the handler within then().<BR>
<a href="http://api.jquery.com/deferred.then">http://api.jquery.com/deferred.then</a><BR>
<BR>
Whereas the Twisted Deferred addCallback() returns the inner Deferred.<BR>
<BR>
Am I getting something wrong?<BR>
<BR>
I'd be glad on any hints/clarification whats going on here ..<BR>
<BR>
Tobias<BR>
<BR>
==<BR>
<BR>
The complete examples are on:<BR>
<BR>
JS:<BR>
<a href="https://github.com/oberstet/Autobahn/blob/master/demo/rpc/simple/simple_client.html">https://github.com/oberstet/Autobahn/blob/master/demo/rpc/simple/simple_client.html</a><BR>
<BR>
Py Client:<BR>
<a href="https://github.com/oberstet/Autobahn/blob/master/demo/rpc/simple/simple_client.py">https://github.com/oberstet/Autobahn/blob/master/demo/rpc/simple/simple_client.py</a><BR>
<BR>
Py Server:<BR>
<a href="https://github.com/oberstet/Autobahn/blob/master/demo/rpc/simple/simple_server.py">https://github.com/oberstet/Autobahn/blob/master/demo/rpc/simple/simple_server.py</a><BR>
<BR>
Library:<BR>
<a href="https://github.com/oberstet/Autobahn/tree/master/lib/python">https://github.com/oberstet/Autobahn/tree/master/lib/python</a><BR>
=&gt; python setup.py install<BR>
<BR>
<BR>
<BR>
<BR>
</SPAN></FONT>
</BODY>
</HTML>