Observed versus promise for single values

Typescript (and now also ECMAScript 2017) gives you great asynchronous tools to use async / await. However, while working with Angular 4, I feel that using Observables is preferred. My question is: when I have a function that returns a single value once (for example: modal confirmation), are there big advantages when using observables or is it preferable to use promises (async / await) / as well? Isn't it strange to use observables? Do they represent a stream of values?

T; dg:

async showDialog(msg: string): Promise<DialogResult>

against.

showDialog(msg: string): Observable<DialogResult>
+4
source share
1 answer

, . Promises, . https://medium.com/@benlesh/rxjs-observable-interop-with-promises-and-async-await-bebb05306875

Observables, .

Promises :

class Whatever {
  resolve: Function;
  reject: Function;
  promise = new Promise((resolve, reject) => {
    this.resolve = resolve;
    this.reject = reject;
  });    

  method() {
    return this.promise;
  }

  otherMethod() {
    this.resolve();
  }
}

Observables Subject:

class Whatever {
  subject = new Subject();

  method() {
    return this.subject.toPromise();
  }

  otherMethod() {
    this.subject.next(...);
    this.subject.complete();
  }
}
0

Source: https://habr.com/ru/post/1686457/


All Articles