RxJS Functions (Part 16)

noop

noop

https://rxjs.dev/api/index/function/noop

A function that does nothing.

This is one of those things that you boggle at until you actually need it for something. I have not needed it for anything.

The best example I could find is actually in AngularJS’s docs:

function foo(callback) {
var result = calculateResult();
(callback || angular.noop)(result);
}

With regard to using it with RxJS, I could see some use case similar to the above when combined with a generator like of.

If you view the console in the example above, you’ll see some variant of the following:

true
7
undefined

Our randOp function will generate a random number, pass the random number to the callback if it exist, and return that value. If there’s no callback, it calls noop which does nothing, producing a return value of undefined.

Then, we wrap the randOp function in of. These creates an Observable that will generate one of the values above.


Thanks for reading. Stay tuned for Part 17, and be sure to checkout my previous articles.