Combine empty, never, and throw Operators with Observables in RxJS

André Staltz
InstructorAndré Staltz
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 3 years ago

This lesson introduces operators empty(), never(), and throw(), which despite being plain and void of functionality, are very useful when combining with other Observables.

[00:00] Next we are going to look at some of the most boring, simple operators there are out there. As we will see later on, observables are meant to be combined, and transformed, and concatenated in many ways, so even the boring observables are useful.

[00:14] Just like with numbers in mathematics, some numbers are quite boring but very important. For instance, the number zero is very boring. It basically means that you have no number. But it's still incredibly important when you combine it with others in some equation or formula.

[00:30] With observables, we have something similar -- that our operator Rx.Observable.empty gives you an empty observable. Let's call that foo. Let's see what it does. If we subscribe to it, what will happen? It says done. It only delivered the completion notification, but it didn't deliver any value.

[00:51] That's because it's equivalent to a create, such that for each observer that subscribes, it just does observer complete. It doesn't deliver any value ever. It just says immediately that it's done. You can think of it as an observable that does nothing. But at least it tells you that it did nothing.

[01:10] That's because there's also another case, another operator called never. What never does, let's check, if we subscribe to it, so far nothing happened. Never is equivalent to Rx.Observable.create. For each observer that subscribes, it does nothing.

[01:33] You can think of it as an infinite observable. That's because the observer that subscribes to this never doesn't know if a value will come soon or not. It's just infinitely expecting for a value. That's because it wasn't called done, so the done was not sent. It means that it's still open for values to be sent.

[01:57] Because the observer doesn't know exactly how the observable works, it's just infinitely there hanging, waiting for a value. There's yet another boring operator called throw which takes a JavaScript error. What it does is it simply throws that error.

[02:16] If you think of it in terms of create, it takes an observer. For each observer that subscribes, it just does observer error. It sends that error there. If we run this, we just see, "Oh, an error happened."

[02:33] As we will see later on, throw, empty, and never, they will be quite useful when we combine them with other operators in RxJS. They exist just as shortcuts so that we don't need to write the create.

egghead
egghead
~ 3 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today