1

I am writing a Javascript App to get data from sensors. I started using ES6 Promises + Generators using Bluebird and the Bluebird.cororutine in both (client and server), but it didn't work fine.

I was advised that Promises didn't work properly in (multiple-event) use cases, and that an alternative could be RxJS.

I've taken a look to RxJS, and it looks like can do the same as the Promises but even better. I wanted to use generators (async/await) to write the async code to look as sync, and my question is:

Can I use RxJS + (async/await) or RxJS has already its own way to do the same as (async/await) ?

Thanks

1 Answers1

0

Async/await is not part of ES6, it's scheduled for ES7. So you're probably not going to use it any time soon in JavaScript. TypeScript supports async/await for ES5 since 2.1.

It think you'll be able to use RxJS with async/await when it comes out. Async/await works with Promises, just like RxJS even though it mostly works with Observables. There are methods such as Observable.toPromise() to convert an Observables to Promises and most Observables also accept a Promise as a parameter.

So I think both are going to be well interchangable (I haven't tried thi personally).

If your primary interest is to make you code more readable than RxJS is a good choice to reduce callback hell.

These two examples show how to call multiple HTTP requests in order using Observable.concatMap() operator. Both examples are written in TypeScript but it should be basically the same in ES6 as well. Also these examples use the new RxJS implementation (https://github.com/ReactiveX/RxJS):

Community
  • 1
  • 1
martin
  • 93,354
  • 25
  • 191
  • 226
  • Thanks for you answer. :). Referent to asyn / await you can use it with babel and bluebird has the coroutine method that does the same. – Toni Benitez Oct 01 '16 at 05:51
  • @ToniBenitez Interesting, I didn't know that :) https://babeljs.io/docs/plugins/transform-async-to-generator/ – martin Oct 01 '16 at 09:54