2

I have a function that calls an http.get to an API and it has a return type of promise. I'd like to handle/sort the returned data. Do I need to convert it from a Promise into an array of objects to iterate over it? Here is the function:

  getCandidates(): Promise<Candidate[]> {   
    return this.http.get(url)
      .retry(2)
      .map(x => {
        var result: Candidate[] = x.json();
        return result;
      })
      .toPromise();
  }

and I call the function by stating:

this.candidates = this.getCandidates();

So I now have this candidates object of type Promise<Candidate[]> but I'd like to have it be of type Candidate[] so I can work with it, e.g. candidates.length etc. How should I go about this? Maybe I'm thinking of this the wrong way.

Edit: trying to use the code.

ionViewDidLoad() {
    //Retrieves candidates and stores into an array
    this.getCandidates().then(candidates => this.candidates = candidates);
    console.log(this.candidates);
  }

The console logs an empty array. I also tried passing it to a sort function but none of the data was present. Which is strange because in the html if I *ngFor over candidates I get the data (which is why I thought it was working properly before).

4
  • You can do it inside map Commented Sep 19, 2017 at 6:01
  • this.getCandidates().then(function(candidates) { ... }); Commented Sep 19, 2017 at 6:02
  • @AlekseyL in map would I just push result onto an array? Commented Sep 19, 2017 at 6:03
  • @ZacharySchwatz, you can sort (or do whatever you need) and return the result Commented Sep 19, 2017 at 6:05

1 Answer 1

1

You can retrieve your candidates in ngOnInit, save it into this.candidates and do what you want to do with it (sorting,..)

  candidates: Candidate[] = [];
  
  ngOnInit() {
    this.getCandidates().then(candidates => this.candidates = candidates);
  }
  
  getCandidates(): Promise<Candidate[]> {   
    return this.http.get(url)
      .retry(2)
      .map(x => x.json as Candidate[])
      .toPromise();
  }

Sign up to request clarification or add additional context in comments.

5 Comments

giving it a shot now, I'm in Ionic so not using ngOnInit but there is ionViewDidLoad which is sufficient in this scenario correct?
I actually have an issue now, when I try to use this.candidates elsewhere in the code or even try to console.log(this.candidates), it is undefined.
You have to show the code where you use this.candidates
It's because of the asynchronous aspect of promise: this.getCandidates().then(candidates => this.candidates = candidates); console.log(this.candidates); In this code, you do console.log(this.candidates) just after calling this.getCandidates(). The console.log is executed before the execution of this.candidates = candidates; this.candidates remains undefined until the promise resolve. If you change your code like below, you'll see this.candidates in console: this.getCandidates().then(candidates => { this.candidates = candidates; console.log(this.candidates); });
thanks, I got it to work that way, instead of calling console log I call my sort function there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.