RxJS + (async/await) for (multiple-event) use cases - javascript

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

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):
Angular 2 + rxjs - how return stream of objects fetched with several subsequent http requests
How to use exhaustMap in ReactiveX/rxjs 5 in TypeScript

Related

Promise.all vs Axios.all

I would like to ask the difference between "Promise.all" & "Axios.all" for several API requests.
I found that both work well in my code, but it is not easy to find its reason..
Could you please explain ?
The Axios documentation on NPM says that Axios.all() is deprecated and you should use Promise.all() in its place. I do not believe there is any intended difference between the two.
In fact, if you look in the current Axios source, you see this:
// Expose all/spread
axios.all = function all(promises) {
return Promise.all(promises);
};
So, they are identical.
I presume that Axios.all() existed historically when Axios wanted to be able to run in environments that didn't have full native promise support so they were supplying promise functionality that might now be present.
Since all modern environments contain Promise.all(), Axios.all() is no longer necessary.

google apps script with UrlfetchApp.fetchAll() or with async/ await for multiple http requests?

I created some projects in Google Apps Script in the past for some automation, that also included some http-fetches. In the past, this worked with .fetch() pretty well, but now we need to fetch multiple urls.
Since apps script now uses V8 runtime I considered to do so with promises. I'm also quite new to async/await and promises in general.
So I considered to try the UrlfetchApp.fetch() within async functions, just to find out, that there's no difference in execution time.
I red, that UrlfetchApp.fetch() will always be sync, no matter whether you declare your function as async or not, due to the GAS-API-design. But I can't find detailed infos on this.
Is this true?
If yes: Then the only way to fetch multiple urls would be UrlfetchApp.fetchAll(), right?
If no: Means simple .fetch() would work inside async funcs (and could be chained in Promise.all()) then I'd invest further time in this.
So, yes or no would help a lot here!
Currently, Urlfetchapp runs synchronously and although the syntax of promises are supported, it works synchronously too.
Then the only way to fetch multiple urls would be UrlfetchApp.fetchAll(), right?
Yes

Coalescing (combining) callbacks in node.js 6

I'm working with an AWS lambda function that has a sort of 'map/reduce' feel to it. But the 'map' part of it, that is the part that does multiple calls is async.
Using the Node 6 STD lib, is there a dynamic way to get all results returned to a shared spot.
What I've thought about so far:
await async is a great abstraction but isn't in node 6 to my knowledge, just node 8.
array.reduce says it takes a callback but the structure of an http request seems not to qualify though I certainly may be wrong
I've thought about a really suboptimal solution, where each callback puts into a shared queue or array. And I have a loop after all the requests that checks the length of the array - I don't like this solution though
Could you guys point me in the right direction or show me some code that would do this?
Bluebird is your friend.
To convert callback functions into promises you can use .promisify() or .fromCallback().
To do map/reduce over an array of promises, you can use .map() and .reduce().

What is the difference between $q.race to Promise.race?

I am using Angularjs version 1.5.7 and $q.race function added in version 1.5.8.
I saw that there is function Promise.race and my question if there is any difference between those functions.
If there is difference should I copy the function from here: AngularJS: $q.race() in old angular versions
and it will works the same like $q.race?
Thanks in advance and sorry for my english!
JavaScript ES6 specifications introduced the method you described above, like Promise.race, Promise.all etc..
The difference is basically that angular has a built-in $q service in order to manage with promises.
This will trigger automatically the digest cycle in order to change everything that could be related to the data returned by the promises or all the involved operations.
If you are allowed to use ES6 specification, you can use directly all the built-in Promises of JavaScript, but then you have to manually trigger the $digest in order to make all the changes happen.
This is one of the main differences between the usage of $q or standard Promises in angularjs.
Moreover, there are a lot of related methods in angular which integrates with $q, like $http for example, and for your tests, you will have your life easier treating directly with $q.
So my suggestion is to use $q if possible.
The core difference is that $q.race will call $rootScope.$apply() under the hood to digest any changes that may occurred after executing your promise callback.
Promise A+ spec has noting to do with that since Promise.all, Promise.race are defined in ES6/ES2015 spec not in Promise A+ spec

AngularJS $promise used with NgResource

I came across promises as I am going through my journey on learning AngularJS and it has been good so far. Now, I wanted to learn more about using AngularJS optional library resource.js. However, I came across examples that confused me a lot. For example, the promise skeleton is basically like this example.
//post method to server
$http.post('api/school',newStudent).then(fetchStudents).then(function(response){
//do something
}, function(error){
//do something
});
and another example like this assuming we have a List factory
List.save(self.newStudent).$promise.then(fetchStudents).then(function(){
self.newStudent = {};
});
So my question is why the use of $promise? What is the use of it? It was not really explained in the book I am using and I am confuse on when to use it or not. Any clear explanation will be appreciated. Thanks
A service that helps you run functions asynchronously, and use their
return values (or exceptions) when they are done processing
Promises provide a simpler alternative for executing, composing, and managing asynchronous operations when compared to traditional callback-based approaches. They also allow you to handle asynchronous errors using approaches that are similar to synchronous try/catch.
Read more about it at :
https://docs.angularjs.org/api/ng/service/$q
http://haroldrv.com/2015/02/understanding-angularjs-q-service-and-promises/
http://chariotsolutions.com/blog/post/angularjs-corner-using-promises-q-handle-asynchronous-calls

Categories