Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Aphorismen
Applications
Business Economics & Admin.
My Computers
Cooking
Devices
Folders
Food
Hardware
Infos
Software Development
Sports
Operation Instructions
Todos
Test
Help
Glossary
Community portal
adaptions
Sidebar anpassen
Wiki RB4
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
JavaScript/JScript
(section)
Page
Discussion
English
Read
Edit
View history
Toolbox
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Observables and Observers==== The observable (e.g. see [https://angular.io/guide/observables here] type can be used to model push-based data sources such as events, timer intervals and sockets. The '''Observer''' pattern is a design pattern in which '''Subjects''' maintain a list of '''Observers''' (consumers) and notifies them of state changes. It is heavily used in [[Angular|Angular]]. The biggest difference between observables and [[JavaScript/JScript#Promises|promises]] is that promises won't change their value once they have been fulfilled. They can only emit (reject, resolve) a single value. On the other hand, observables can emit multiple results. The subscriber will be receiving results until the observer is completed or unsubscribed from. A handler for receiving observable notifications implements the '''Observer''' interface. It is an object that defines callback methods to handle the three types of notifications that an observable can send: Observer = { next: ... // required error: ... // optional complete: ... // optional * An observable is said to be '''cold''' because it does not generate new values if no subscriptions exist * '''$''' suffix (popularized by Cycle.js) is used to indicate that the variable is an Observable. * '''Creation Operators''' are the other kind of operator, which can be called as standalone functions to create a new Observable. // create an observable timer$ = new Observable<string>( // new observable observer => { // '''subscriber function''' is executed when a consumer calls subscribe() and desribes how to generate values setInterval( () => observer.next(new Date().toString()), 10000); console.log("someone subscribed"); } ); var subscription: Subscription = timer$.subscribe({ // passing an '''observer''' which is an objects that defines the handler for the values it receives next(datestring) { // implements the Observer interface with callback methods for the 3 types of notifications console.log(datestring); // next, error and complete } });
Summary:
Please note that all contributions to Wiki RB4 may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Uwe Heuer Wiki New:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Toggle limited content width