diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 32b8ff541fb03..acac4d5dd5849 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -867,6 +867,7 @@ testing/** @angular/fw-test /aio/content/guide/observables.md @angular/fw-docs-observables @angular/framework-global-approvers @angular/framework-global-approvers-for-docs-only-changes /aio/content/examples/observables/** @angular/fw-docs-observables @angular/framework-global-approvers @angular/framework-global-approvers-for-docs-only-changes /aio/content/guide/comparing-observables.md @angular/fw-docs-observables @angular/framework-global-approvers @angular/framework-global-approvers-for-docs-only-changes +/aio/content/examples/comparing-observables/** @angular/fw-docs-observables @angular/framework-global-approvers @angular/framework-global-approvers-for-docs-only-changes /aio/content/guide/observables-in-angular.md @angular/fw-docs-observables @angular/framework-global-approvers @angular/framework-global-approvers-for-docs-only-changes /aio/content/examples/observables-in-angular/** @angular/fw-docs-observables @angular/framework-global-approvers @angular/framework-global-approvers-for-docs-only-changes /aio/content/guide/practical-observable-usage.md @angular/fw-docs-observables @angular/framework-global-approvers @angular/framework-global-approvers-for-docs-only-changes diff --git a/aio/content/examples/comparing-observables/src/observables.ts b/aio/content/examples/comparing-observables/src/observables.ts new file mode 100644 index 0000000000000..f0e8c1a138f87 --- /dev/null +++ b/aio/content/examples/comparing-observables/src/observables.ts @@ -0,0 +1,40 @@ +import { map } from 'rxjs/operators'; +import { Observable } from 'rxjs'; + +// #docregion observable + +// declare a publishing operation +const observable = new Observable(observer => { + // Subscriber fn... +}); + +// initiate execution +observable.subscribe(() => { + // observer handles notifications +}); + +// #enddocregion observable + +// #docregion unsubscribe + +const subscription = observable.subscribe(() => { + // observer handles notifications +}); + +subscription.unsubscribe(); + +// #enddocregion unsubscribe + +// #docregion error + +observable.subscribe(() => { + throw Error('my error'); +}); + +// #enddocregion error + +// #docregion chain + +observable.pipe(map(v => 2 * v)); + +// #enddocregion chain diff --git a/aio/content/examples/comparing-observables/src/promises.ts b/aio/content/examples/comparing-observables/src/promises.ts new file mode 100644 index 0000000000000..0ad68d99dafa4 --- /dev/null +++ b/aio/content/examples/comparing-observables/src/promises.ts @@ -0,0 +1,25 @@ +// #docregion promise +// initiate execution +const promise = new Promise((resolve, reject) => { + // Executer fn... +}); + +promise.then(value => { + // handle result here +}); + +// #enddocregion promise + +// #docregion chain + +promise.then(v => 2 * v); + +// #enddocregion chain + +// #docregion error + +promise.then(() => { + throw Error('my error'); +}); + +// #enddocregion error diff --git a/aio/content/guide/comparing-observables.md b/aio/content/guide/comparing-observables.md index 8179e43bfbc2a..c3402b1644bbc 100644 --- a/aio/content/guide/comparing-observables.md +++ b/aio/content/guide/comparing-observables.md @@ -21,48 +21,47 @@ Observables are often compared to promises. Here are some key differences: * Observables are not executed until a consumer subscribes. The `subscribe()` executes the defined behavior once, and it can be called again. Each subscription has its own computation. Resubscription causes recomputation of values. - -// declare a publishing operation -new Observable((observer) => { subscriber_fn }); -// initiate execution -observable.subscribe(() => { - // observer handles notifications -}); - + + * Promises execute immediately, and just once. The computation of the result is initiated when the promise is created. There is no way to restart work. All `then` clauses (subscriptions) share the same computation. - -// initiate execution -new Promise((resolve, reject) => { executer_fn }); -// handle return value -promise.then((value) => { - // handle result here -}); - + + ### Chaining * Observables differentiate between transformation function such as a map and subscription. Only subscription activates the subscriber function to start computing the values. - -observable.pipe(map((v) => 2*v)); - + + * Promises do not differentiate between the last `.then` clauses (equivalent to subscription) and intermediate `.then` clauses (equivalent to map). - -promise.then((v) => 2*v); - + + ### Cancellation * Observable subscriptions are cancellable. Unsubscribing removes the listener from receiving further values, and notifies the subscriber function to cancel work. - -const sub = obs.subscribe(...); -sub.unsubscribe(); - + + * Promises are not cancellable. @@ -70,19 +69,19 @@ sub.unsubscribe(); * Observable execution errors are delivered to the subscriber's error handler, and the subscriber automatically unsubscribes from the observable. - -obs.subscribe(() => { - throw Error('my error'); -}); - + + * Promises push errors to the child promises. - -promise.then(() => { - throw Error('my error'); -}); - + + ### Cheat sheet @@ -100,14 +99,16 @@ The following code snippets illustrate how the same kind of operation is defined Creation -
new Observable((observer) => {
-    observer.next(123);
-  });
+
+new Observable((observer) => {
+  observer.next(123);
+});
-
new Promise((resolve, reject) => {
-    resolve(123);
-  });
+
+new Promise((resolve, reject) => {
+  resolve(123);
+});
@@ -118,14 +119,16 @@ The following code snippets illustrate how the same kind of operation is defined Subscribe -
sub = obs.subscribe((value) => {
-    console.log(value)
-  });
+
+sub = obs.subscribe((value) => {
+  console.log(value)
+});
-
promise.then((value) => {
-    console.log(value);
-  });
+
+promise.then((value) => {
+  console.log(value);
+});
@@ -165,7 +168,6 @@ subscription.unsubscribe();
function handler(e) {
   console.log(‘Clicked’, e);
 }
-
 // Setup & begin listening
 button.addEventListener(‘click’, handler);
 // Stop listening