I have a big problem with Javascript (I'm developing a web application with Angular 4 using TypeScript language. Backend side is developed using AWS). I don't know how to use the asynchronous call mechanism very well.
Unfortunately if I using promises I can not use any object in the code.
I wish manipulate them in my methods. But the only thing I can do is use this data for a static display in the html template, and don't manipulate them in the code. This is an example of my code:
// In ClassA
getCognitoUsers() {
var params = {
UserPoolId: environment.userPoolId
};
return this.cognitoIdentityServiceProvider.listUsers(params).promise();
}
// In ClassB
let container = this;
this.ClassB.getCognitoUsers().then(
function(data) {
container.myProperty = data;
}
);
The object this.myProperty
it can also be refered by other classes in the application. The problem is that I can do what I want in the html template, but not in the business code, such this:
// In the HTML template
{{myProperty.mySubProperty}} <!-- displays correctly! -->
// In the .ts file
myMethod() {
let subProperty = this.myProperty.mySubProperty; // ERROR: will always be undefined because not in asynchronous flow
}
I know that the problem is this because I called an asynchronous method, but I hoped that with the promises I would have resolved the situation. Someone can tell me what's wrong in this code and how can I get the object to manipulate in the business code (and not only statically in the template)? I would like some important operations of the application to be performed AFTER the asynchronous call!