2018-01-09 19:31:41 +00:00
# Observables compared to other techniques
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
You can often use observables instead of promises to deliver values asynchronously.
Similarly, observables can take the place of event handlers.
Finally, because observables deliver multiple values, you can use them where you might otherwise build and operate on arrays.
2018-01-09 19:31:41 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
Observables behave somewhat differently from the alternative techniques in each of these situations, but offer some significant advantages.
Here are detailed comparisons of the differences.
2018-01-09 19:31:41 +00:00
## Observables compared to promises
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
Observables are often compared to promises.
Here are some key differences:
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
* Observable execution is deferred; computation does not start until subscription.
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
Promises execute immediately on creation.
This makes observables useful for defining recipes that can be run whenever you need the result.
2018-01-09 19:31:41 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
* Observables provide many values.
Promises provide one.
This makes observables useful for getting multiple values over time.
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
* Observable values can be transformed with operators as well as in the subscription. The rich variety of RxJS operators observables enables complex transformations that can be passed around to other parts of the system, without causing the work to be executed prematurely.
Promises have `.then()` clauses which can transform values but only after the work is done.
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
* Observables and Promises handle errors differently with roughly comparable efficacy.
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
The following sections explore these points in greater detail.
2018-01-09 19:31:41 +00:00
### Creation and subscription
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
* Observables are not executed until a consumer subscribes.
2023-08-25 09:06:52 +00:00
The `subscribe()` initiates the observable's behavior which may execute synchronously or asynchronously and could produce one, many or no values over time.
For "unicast" observables, if you call `subscribe` again, you get a new observable execution with its own production of values.
Calling `subscribe` on a "multicast" observable (e.g., `Subject` or an observable with the `shareReplay` operator) simply adds another *subscriber* to the already running observable.
The `subscribe` call is the end-of-the-line. You cannot continue to manipulate values after `subscribe(...)` .
2018-01-09 19:31:41 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/observables.ts (observable)" path = "comparing-observables/src/observables.ts" region = "observable" > < / code-example >
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
* Promises execute immediately when they are created. There is no deferred execution and, therefore, no equivalent to `subscribe()` .
A promise is always asynchronous and can produce at most one value.
There is no way to restart a promise and it retains its result value for the life of the promise.
You can chain additional `then` clauses to a promise.
2018-01-09 19:31:41 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/promises.ts (promise)" path = "comparing-observables/src/promises.ts" region = "promise" > < / code-example >
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
< a id = "chaining" > < / a >
### Transformations
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
* Developers can transform values both in the *subscription* and in piped *operators* . There are large number of RxJS operators to suit many complex scenarios, including numerous ways to combine and split observables.
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
< code-example header = "src/observables.ts (operators and multiple values)" path = "comparing-observables/src/observables.ts" region = "operators" > < / code-example >
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
* Promises do not have an equivalent to `subscribe()` . You can transform the emitted value of a promise through one or more `.then` clauses. Promises have a small set of combiners (e.g., `all` , `any` , `race` ).
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
< code-example header = "src/promises.ts (chained .then)" path = "comparing-observables/src/promises.ts" region = "chain" > < / code-example >
2018-01-09 19:31:41 +00:00
### Cancellation
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
* Observable subscriptions are cancellable.
Unsubscribing removes the listener from receiving further values, and notifies the subscriber function to cancel work.
2018-01-09 19:31:41 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/observables.ts (unsubscribe)" path = "comparing-observables/src/observables.ts" region = "unsubscribe" > < / code-example >
2018-01-09 19:31:41 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
* Promises are not cancellable.
2018-01-09 19:31:41 +00:00
### Error handling
2023-08-25 09:06:52 +00:00
* Observable execution errors can be handled with the `catchError()` operator or in the `subscribe` .
`catchError` can put the observable back on the normal path where it continues to produce values or it can rethrow the error. An uncaught error unsubscribes all subscribers.
2018-01-09 19:31:41 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/observables.ts (error)" path = "comparing-observables/src/observables.ts" region = "error" > < / code-example >
2018-01-09 19:31:41 +00:00
2023-08-25 09:06:52 +00:00
* Promise errors can be handled with a `.catch()` or in the second argument of a `.then()` .
2018-01-09 19:31:41 +00:00
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
< code-example header = "src/promises.ts (error)" path = "comparing-observables/src/promises.ts" region = "error" > < / code-example >
2018-01-09 19:31:41 +00:00
### Cheat sheet
The following code snippets illustrate how the same kind of operation is defined using observables and promises.
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
| Operation | Observable | Promise |
|:--- |:--- |:--- |
| Creation | < code-example format = "typescript" hideCopy language = "typescript" > new Observable((observer) => { 
 observer.next(123); 
 }); < / code-example > | < code-example format = "typescript" hideCopy language = "typescript" > new Promise((resolve, reject) => { 
 resolve(123); 
 }); < / code-example > |
| Transform | < code-example format = "typescript" hideCopy language = "typescript" > obs.pipe(map((value) => value * 2));< / pre > | < code-example format = "typescript" hideCopy language = "typescript" > promise.then((value) => value * 2);< / code-example > |
| Subscribe | < code-example format = "typescript" hideCopy language = "typescript" > sub = obs.subscribe((value) => { 
 console.log(value) 
 });< / code-example > | < code-example format = "typescript" hideCopy language = "typescript" > promise.then((value) => { 
 console.log(value); 
 }); < / code-example > |
| Unsubscribe | < code-example format = "typescript" hideCopy language = "typescript" > sub.unsubscribe();< / code-example > | Implied by promise resolution. |
2018-01-09 19:31:41 +00:00
## Observables compared to events API
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
Observables are very similar to event handlers that use the events API.
Both techniques define notification handlers, and use them to process multiple values delivered over time.
Subscribing to an observable is equivalent to adding an event listener.
One significant difference is that you can configure an observable to transform an event before passing the event to the handler.
2018-01-09 19:31:41 +00:00
Using observables to handle events and asynchronous operations can have the advantage of greater consistency in contexts such as HTTP requests.
Here are some code samples that illustrate how the same kind of operation is defined using observables and the events API.
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
| | Observable | Events API |
|:--- |:--- |:--- |
| Creation & cancellation | < code-example format = "typescript" hideCopy language = "typescript" > // Setup 
 const clicks$ = fromEvent(buttonEl, 'click'); 
 // Begin listening 
 const subscription = clicks$ 
 .subscribe(e => console.log('Clicked', e)) 
 // Stop listening 
 subscription.unsubscribe(); < / code-example > | < code-example format = "typescript" hideCopy language = "typescript" > function handler(e) { 
 console.log('Clicked', e); 
 } 
 // Setup & begin listening 
 button.addEventListener('click', handler); 
 // Stop listening 
 button.removeEventListener('click', handler); < / code-example > |
| Subscription | < code-example format = "typescript" hideCopy language = "typescript" > observable.subscribe(() => { 
 // notification handlers here 
 });< / code-example > | < code-example format = "typescript" hideCopy language = "typescript" > element.addEventListener(eventName, (event) => { 
 // notification handler here 
 }); < / code-example > |
| Configuration | Listen for keystrokes, but provide a stream representing the value in the input. < code-example format = "typescript" hideCopy language = "typescript" > fromEvent(inputEl, 'keydown').pipe( 
 map(e => e.target.value) 
 ); < / code-example > | Does not support configuration. < code-example format = "typescript" hideCopy language = "typescript" > element.addEventListener(eventName, (event) => { 
 // Cannot change the passed Event into another 
 // value before it gets to the handler 
 }); < / code-example > |
2018-01-09 19:31:41 +00:00
## Observables compared to arrays
docs: improve markdown (#45325)
The purpose of the changes is to clean all markdown to match a single pedantic style.
* To ensure all changes in style are properly separated.
* To ensure all styled content aligns to nearest 4-character-tab.
* To ensure all code blocks use the Angular `<code-example>` or `<code-tab>` elements.
* To ensure all markdown exists outside of html tags.
* To ensure all images use the Angular style for `<img>` elements.
* To ensure that all smart punctuation is replaced or removed.
```text
’, ’, “, ”, –, —, …
```
* To ensure all content does not conflict with the following reserved characters.
```text
@, $, *, &, #, |, <, >,
```
* To ensure all content displays using html entities.
The following changes were made to files in the following directory.
```text
aio/content
```
The target files were markdown files.
The list of excluded files:
```text
.browserslistrc, .css, .conf, .editorconfig, .gitignore, .html, .js, .json, .sh, .svg, .ts, .txt, .xlf,
```
PR Close #45325
2022-03-10 16:48:09 +00:00
An observable produces values over time.
An array is created as a static set of values.
In a sense, observables are asynchronous where arrays are synchronous.
In the following examples, < code > → < / code > implies asynchronous value delivery.
| Values | Observable | Array |
|:--- |:--- |:--- |
| Given | < code-example format = "typescript" hideCopy language = "typescript" > obs: → 1→ 2→ 3→ 5→ 7 < / code-example > < code-example format = "typescript" hideCopy language = "typescript" > obsB: → 'a'→ 'b'→ 'c' < / code-example > | < code-example format = "typescript" hideCopy language = "typescript" > arr: [1, 2, 3, 5, 7] < / code-example > < code-example format = "typescript" hideCopy language = "typescript" > arrB: ['a', 'b', 'c'] < / code-example > |
| `concat()` | < code-example format = "typescript" hideCopy language = "typescript" > concat(obs, obsB) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > → 1→ 2→ 3→ 5→ 7→ 'a'→ 'b'→ 'c' </ code-example > | < code-example format = "typescript" hideCopy language = "typescript" > arr.concat(arrB) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > [1,2,3,5,7,'a','b','c'] </ code-example > |
| `filter()` | < code-example format = "typescript" hideCopy language = "typescript" > obs.pipe(filter((v) => v> 3)) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > → 5→ 7 </ code-example > | < code-example format = "typescript" hideCopy language = "typescript" > arr.filter((v) => v> 3) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > [5, 7] </ code-example > |
| `find()` | < code-example format = "typescript" hideCopy language = "typescript" > obs.pipe(find((v) => v> 3)) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > → 5 </ code-example > | < code-example format = "typescript" hideCopy language = "typescript" > arr.find((v) => v> 3) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > 5 </ code-example > |
| `findIndex()` | < code-example format = "typescript" hideCopy language = "typescript" > obs.pipe(findIndex((v) => v> 3)) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > → 3 </ code-example > | < code-example format = "typescript" hideCopy language = "typescript" > arr.findIndex((v) => v> 3) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > 3 </ code-example > |
| `forEach()` | < code-example format = "typescript" hideCopy language = "typescript" > obs.pipe(tap((v) => { 
 console.log(v); 
 })) 
 1 
 2 
 3 
 5 
 7 </ code-example > | < code-example format = "typescript" hideCopy language = "typescript" > arr.forEach((v) => { 
 console.log(v); 
 }) 
 1 
 2 
 3 
 5 
 7 </ code-example > |
| `map()` | < code-example format = "typescript" hideCopy language = "typescript" > obs.pipe(map((v) => -v)) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > → -1→ -2→ -3→ -5→ -7 </ code-example > | < code-example format = "typescript" hideCopy language = "typescript" > arr.map((v) => -v) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > [-1, -2, -3, -5, -7] </ code-example > |
| `reduce()` | < code-example format = "typescript" hideCopy language = "typescript" > obs.pipe(reduce((s,v)=> s+v, 0)) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > → 18 </ code-example > | < code-example format = "typescript" hideCopy language = "typescript" > arr.reduce((s,v) => s+v, 0) </ code-example > < code-example format = "typescript" hideCopy language = "typescript" > 18 </ code-example > |
<!-- links -->
<!-- external links -->
<!-- end links -->
2023-08-25 09:06:52 +00:00
@reviewed 2023-08-25