Ram's
Ram's

Reputation: 121

Pass first method return value to another method in Typescript for Angular2

I am new to typescript and would like to pass one method return value to another method. Because second method is dependent on first method return value. Below is my code:

// First method call
this.someService.getLoggedinUser()
    .subscribe(uid => {
        console.log(uid);
        this.uid = uid;
    });

//Second method call
this.someService.getUser(this.uid.toUpperCase())
    .subscribe(user => {
        this.user = user;
    });

I am trying to pass mehtod 1 return value to method 2 something like:

constructor(private someService:SomeService){
this.someService.getUser(uid: string  => {
   this.someService.getLoggedinUser()
      .map(uid => {
          return uid.toUpperCase();
      });
  })
  .subscribe(user => {
        this.user = user;
    }); }

Please help me to achieve this.

Upvotes: 1

Views: 2130

Answers (5)

sainu
sainu

Reputation: 2701

The best method in angular2 You can do this using flatMap

Then here is how you chain two calls:

private someMethod{
    this.someService.getLoggedinUser()
       .subscribe(uid => {
               console.log(uid);
               this.uid = uid;
            })
            .flatMap((uid) => 
                this.someService.getUser(this.uid.toUpperCase())
                   .subscribe(user => {
                        this.user = user;
            });
     }

Upvotes: 2

shaunak1111
shaunak1111

Reputation: 961

You can use nested subscribe calls to achieve this.

// assigning self = this for debugging purpose when typing variable in console
// example typing self.uid gives value inside subscribe but this.uid is 
//undefined.

let self = this; 
self.someService.getLoggedinUser()
.subscribe(uid => {
    console.log(uid);
    self.uid = uid;
    //Second method call
    self.someService.getUser(uid.toUpperCase())
     .subscribe(user => {
         self.user = user;
    });
});

Upvotes: 0

LoïcR
LoïcR

Reputation: 5039

You could just call the second method in the Observable of your element request.

public test() {
    this.someService
        .getLoggedinUser()
        .subscribe(uid => {
            const uppercaseUid = uid.toUpperCase();
            this.someService
                .getUser(uppercaseUid)
                .subscribe(user => this.user = user);
        });
}

Upvotes: 0

Ahmed Musallam
Ahmed Musallam

Reputation: 9753

Here is a simple form:

var user;

this.someService.getLoggedinUser()
    .subscribe(uid => this.someService.getUser(uid.toUpperCase())
                      .subscribe(user => this.user = user));

Upvotes: 0

Rahul Singh
Rahul Singh

Reputation: 19622

you can assign the value returned by the first method call to a local var and then use it in the next method i guess or you can do the same in the first observable method like this.

i used this in my repo https://github.com/rahulrsingh09/AngularConcepts

getLukeSkywalkerObservable(){
    return this.http.get('http://swapi.co/api/people/1/')
      .map(res => {
        return  res.json(); // using maps to filter data returned form the http call
      }).map(data => {
        return data; // using maps of maps to filter data returned form the map
      }).flatMap((jedi) => this.http.get(jedi.homeworld))
      .map(res => {
        return res.json().name; // using flat maps to combine data returned from two observables into one
      }).catch((error:any) => Observable.throw(error.json().error || 'Server error'));
  }

Upvotes: 0

Related Questions