Guest User

Untitled

a guest
May 26th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. Ensuring Cleanup with Finally
  2.  
  3. We've already covered the try/catch part of try/catch/finally, so what about finally? We have the finally operator which calls a function after the source sequence terminates gracefully or exceptionally. This is useful if you are using external resources or need to free up a particular variable upon completion.
  4.  
  5. In this example, we can ensure that our WebSocket will indeed be closed once the last message is processed.
  6.  
  7. var socket = new WebSocket('ws://someurl', 'xmpp');
  8.  
  9. var source = Rx.Observable.from(data)
  10. .finally(() => socket.close());
  11.  
  12. var subscription = source.subscribe(
  13. data => {
  14. socket.send(data);
  15. }
  16. );
Add Comment
Please, Sign In to add comment