0

What is the equivalent of Promise.resolve() (resolving without any value is important) ?

There is a similar question here : rxjs alternative to doing a Promise.resolve?.

The given solution (Observable.of(data)) works well if we provide any data but this doesn't work if we don't provide a value which make sense to me as there is no value, so .subscribe() is not fired.

In an Angular2 service of an ionic2 app, I'm trying to migrate a Promise based service to Observables. If device is offline, I need to skip some steps of a sequence. With promises, I was throwing an Error and catching it later in the sequence :

return this.connectivityService.isOnline()
  .flatMap((isOnline: boolean) => {
    if (isOnline) {
      return Promise.resolve(); // work as expected, subscribe() is called
    } else {
      return Observable.throw(new OfflineError('The device is offline.'));
    }
  })
  .do(() => console.log('Do things here'));

The following code doesn't:

return this.connectivityService.isOnline()
  .flatMap((isOnline: boolean) => {
    if (isOnline) {
      return Observable.of(); // subscribe() is not called
    } else {
      return Observable.throw(new OfflineError('The device is offline.'));
    }
  })
  .do(() => console.log('Do things here'));

Is it possible to achieve the same thing with observables ? Feel free to tell me if I have a bad usage of observables, this is something new for me.

Community
  • 1
  • 1
bgondy
  • 1,198
  • 3
  • 20
  • 32

1 Answers1

1

To answer the question in a direct manner:

The equivalent of Promise.resolve() would be Observable.of(undefined) - but usually you can avoid such workarounds.

Did you simplify your code? Because in your case you don't actually have to use a flatMap here, you could also simply throw an error directly:

return this.connectivityService.isOnline()
  .do((isOnline: boolean) => {
    if (!isOnline) {
      throw new OfflineError('The device is offline.');
    }
  })
  .do(() => console.log('Do things here'));

const isOnline$ = new Rx.BehaviorSubject(false); // some online-check-service-mock

isOnline$
  .do(isOnline => {
    if (!isOnline) {
      throw new Error("You are offline!");  
    }
  })
  .do(() => console.log('Do things here'))
  .subscribe(
    console.log,
    error => console.error("Oops! ", error.message),
    console.info
  );
<script src="https://unpkg.com/rxjs/bundles/Rx.min.js"></script>
olsn
  • 16,644
  • 6
  • 59
  • 65
  • Yes the code snippet is simplified, When online, I fetch data from a remote API and I update my local database, I download some files so they can be available offline etc. Then, I fetch data from my local database to render it (more or less), following the concept of "offline first" apps. – bgondy Feb 09 '17 at 10:42
  • `Observable.of(undefined)` is working as expected. I was looking for a specific operator but i was wrong. Thank you! – bgondy Feb 09 '17 at 10:46
  • Okay, but you could still to the fetching of stuff when online in an appended `flatMap` - there really should not be the need of the `Observable.of`-workaround here. – olsn Feb 09 '17 at 10:59
  • I'm learning observables so you are probably right, I will investigate this. Thank you! – bgondy Feb 09 '17 at 11:15