Yongquan Yongquan - 5 months ago 18
Javascript Question

How is it that multiple pipe() methods can be chained?

I've been struggling with some JQuery code from MS, I don't quite understand the following code:

$.when(......)
.pipe(a())
.pipe(b())
.pipe(c());

a = function(){new $.Deferred().resolve();};
b = function(){d();};
c = ... //some code


The code actually works very well, but I think neither a() nor b() is returning a deferred or promised object, so how come those methods can be chained together?

Answer

The functions called by .pipe() are not requred to return a Deferred or Promise. From the documentation

These filter functions can return a new value to be passed along to the piped promise's done() or fail() callbacks, or they can return another observable object (Deferred, Promise, etc) which will pass its resolved / rejected status and values to the piped promise's callbacks.

In your code, since they don't have return statements, it's equivalent to return undefined;, and this is taken as the "new value to be passed along".

Comments