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.