Anyone have any thoughts on Observables vs. promises or other async methods? They seem kinda painful to me, but I don't have much experience with them to know the benefits.
This is some example code from a new microservice starter that someone at my work came up with (based on
this typescript node starter library or a similar one I'm sure):
Code:
export class StarwarsService {
public getPeopleById(id: number): Observable<People> {
const loadedCharacter: AsyncSubject<People> = new AsyncSubject<People>();
const url1_options = {
method: 'GET',
uri: 'http://swapi.co/api/people/' + id,
resolveWithFullResponse: true,
json: true,
time: true,
timeout: process.env.TIME_OUT
};
const url2_options = {
method: 'GET',
uri: 'http://swapi.co/api/planets/' + id,
resolveWithFullResponse: true,
json: true,
time: true,
timeout: process.env.TIME_OUT
};
const character: Observable<People> = Observable.fromPromise(rp(url1_options));
const characterHomeworld: Observable<Planet> = Observable.fromPromise(rp(url2_options));
Observable.forkJoin([character, characterHomeworld]).subscribe(
results => {
// const result_0: People = JSON.parse(results[0]);
// const result_1: Planet = JSON.parse(results[1]);
LOG.info(url1_options.uri, results[0].timings);
LOG.info(url2_options.uri, results[1].timings);
const result_0: People = results[0].body;
const result_1: Planet = results[1].body;
result_0.homeworld = result_1;
loadedCharacter.next(result_0);
loadedCharacter.complete();
},
err => {
loadedCharacter.next(undefined);
loadedCharacter.complete();
});
return loadedCharacter;
}
}
Seems like a lot of code to essentially just make two API calls in parallel. Also I'm not sure I want to give offshore devs who just want to do some business logic an example that includes Observable.forkJoin().
Beyond that on a higher level - I'm not really sold on typescript with node. Auto-complete is awesome, compiler checking is pretty handy. But I haven't run into very many bugs or sticky situations in node that would have been prevented by strict type checking, and I don't like how webpack slows it down.
But mostly I'm worried that it tends to nudge node into a becoming a rails or java app - which kills a lot of the free-form loosey goosey advantages of programming in node. Doesn't help that the devs pushing this stuff at my company (this is the 2nd one I've had to deal with) are all Java developers.
It's also got MVC all over the place which I don't like in general and think is total overkill for a microservice.
Thoughts?