Is there something like q.all/.forkJoin to resolve all http api requests/local db requests in NS Angular?


#1

Hey all!

In angular1, We can do something like this:

var promises = [api.getA(),api.getB()];
$q.all(promises).then(function(response){
    // response[0]  --> A
    // response[1]  --> B
})

Nativescript http module returns Observable,

api.getA().subscribe(A => {A})
api.getB().subscribe(B => {B})

But I want to resolve and handle A and B together like same response.

So what is the workaround need on this!

cheers!


#2

I think mergeMap works here.

Update: forkJoin should work until your api calls are not depended on each other.


#3

Here is an example.


#4

mergeMap is working fine.

Actually i need subscribe multiple different api calls response into single response. Then i may access those simply as mentioned,

 .subscribe((response) => {
                console.log(response[0], response[1], response[2]);
            });

This is how i tried to use forkJoin. But i am not getting!


#5

You have add / import forkJoin operator to your project.

The statement below will add forkJoin static method to the Observable class.

import "rxjs/add/observable/forkJoin";

Or you can import it as

import { forkJoin } from "rxjs/observable/forkJoin";