0

I'm developing a web application with Angular 4 (using TypeScript / JavaScript language). Unfortunately I don't know JavaScript and its callback mechanism very well. I have a problem. If I call a method that takes a function as a parameter, such this:

    this.cognitoIdentityServiceProvider.listUsers(params,function(err,data) {
       if (err) console.log(err, err.stack);
       else {
         console.log(data);      
    }

I can use the data parameter only inside the body of function(err,data) and not elsewhere.

If I initialize an external object inside function(err,data), it seems that the object only takes the right value within the function, but not externally, as in this example:

var myData = null; // external variable declaration

this.cognitoIdentityServiceProvider.listUsers(params,function(err,data) {
   if (err) console.log(err, err.stack);
   else {
     myData = data;
     console.log(myData); // prints the date value (correctly)  
}
console.log(myData); // print 'null' but I would like the data of 'data' object 

I would be very comfortable with using this data outside function(err,data) to pass them as a parameter. Unfortunately I have no idea how to do it.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
claudioz
  • 1,121
  • 4
  • 14
  • 25

1 Answers1

1

You cannot do that. You would either have to wrap your callback in a promise and the access it in a .then() or pass the variable using a callback. You can't expect it to access it synchronously when asynchronous code is present You can use the code

function PromiseWrapped(){
    return new Promise(function(resolve, reject)){
        this.cognitoIdentityServiceProvider.listUsers(params,function(err,data) {
           if (err) reject(err)
           else {
             resolve(data);
        }
    }
}

PromiseWrapped().then(function(data){
    console.log(data)
})
marvel308
  • 10,288
  • 1
  • 21
  • 32
  • Thanks, could you give me an example of code? I do not know the mechanism of synchronous and asynchronous JavaScript calls – claudioz Sep 22 '17 at 09:59
  • check out the answer at https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – marvel308 Sep 22 '17 at 10:03
  • thanks, but the example is too long and I can't make it match with my situation because my method listUser takes two input parameters and not just the function! If you wrote me how I can customize my code you would do me a favor! – claudioz Sep 22 '17 at 10:43
  • edited the answer – marvel308 Sep 22 '17 at 10:48
  • Give me an error: Error: Uncaught (in promise): TypeError: Cannot read property 'cognitoIdentityServiceProvider' of undefined – claudioz Sep 22 '17 at 12:23
  • check the scope of this, I'm pretty sure you're referring to the wrong this – marvel308 Sep 22 '17 at 12:25
  • I don't explain why the property 'this' appears to be unknown in this scope! – claudioz Sep 22 '17 at 12:27
  • Try printing what this refers to – marvel308 Sep 22 '17 at 12:28
  • It prints the properties of my class if I use console.log(this) outside the function written by you; otherwise print undefined (if i use console.log(this) in the scope of function PromiseWrapped() ) – claudioz Sep 22 '17 at 12:33
  • You can pass this as an argument in your case, in a variable called that – marvel308 Sep 22 '17 at 12:38
  • Ok, it works, but the problem persists. How do I use the "data" object outside of the body of "PromiseWrapped().then(function(data)" ? I would like to use it to assign it to a data field or return it to another class, but I can not! – claudioz Sep 22 '17 at 12:52
  • If you want the code to act synchronously, you can't. You can update inside the .then() and then use it in subsequent .then() functions – marvel308 Sep 22 '17 at 13:27