Reputation: 13
This could be (and probably is) a simple question, but I'm really confused with how to handle promises. I've been reading a lot about it, but still can't completely get it. I'm working with Typescript and Angular and I'm requesting data from an API REST. I have a method getServices, which returns an array with the information I get from the API.
My problem is when I call this method here:
this.getServices = (query) => {
return this.source.getServices(query)
.then(this.transformToSegments(false));
};
I'm having an error TypeError: results is undefined.
results is used by transformToSegments. I put a lot of console.log() to debug and I saw that transformToSegments is executing before getServices gets resolved, so it tries to work on an undefined result. I'm obviously doing something wrong, but I haven't been able to fix it. Any help is appreciated!
Thanks in advance,
Julia
Upvotes: 1
Views: 67
Reputation: 3128
This is a common mistake. You are passing the result of this.transformToSegments(false)
to the .then()
callback. I assume you want something more like:
.then(results => this.transformToSegments(false, results))
This will delay the execution of the transform until the then
is called.
Upvotes: 2