0

I have a question about JavaScript language (unfortunately I do not know it very well). To realize my web application (based on AWS Cognito), I'm using asynchronous methods, like this:

this.cognitoIdentityServiceProvider.listUsers(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else   {  console.log(data) // successful response
  }
});

My problem is that I absolutely need to save the "data object" in a data field of a class to use it and graphically display in the frontend of the application. Unfortunately I try a lot od days, but I could not copying these important data in any way (I need it as data fields of my class!!). I tried with the promises and the callbacks but I could not get anything!

It's possible can convert this asynchronous method to a synchronous method and wait for the application until the data object is recovered? If is impossibilet, how can I get this data object as a data field in my class?

Thanks a lot!

Roamer-1888
  • 19,138
  • 5
  • 33
  • 44
claudioz
  • 1,121
  • 4
  • 14
  • 25
  • 1
    Yes you can achieve this the way you described: callbacks or promises! – kevinSpaceyIsKeyserSöze Sep 25 '17 at 08:00
  • The problem is that I can not. If I try to assign the value to a data field, it is not copied except in the body of the function! Could you write me sample code? – claudioz Sep 25 '17 at 08:05
  • No, asynchronous code cannot be converted to synchronous code. You simply have to do whatever you have to do inside that callback. You can assign the value to any property inside that callback too. – deceze Sep 25 '17 at 08:10
  • But this value is not persistent. if I assign a value to a class data field inside the callback, it will return undefined and will not be able to be displayed! – claudioz Sep 25 '17 at 08:15
  • 1
    Only if you try to display it before you assign it. The code in the callback happens *sometime later*; that's what *asynchronous* means. It's not more or less "persistent". – deceze Sep 25 '17 at 08:22
  • Ok, but how can I display it only after I make sure it has been assigned? That's what I have to do but I can not – claudioz Sep 25 '17 at 08:26
  • With asynchronous code you need to do everything you need to do *in the callback*. You essentially "pause" all your code until the asynchronous part resolves and the callback is called and then continue doing whatever it is you're doing in the callback. – deceze Sep 25 '17 at 08:30
  • Thank you very much, you could make me see a small example because unfortunately I have very little time to learn this mechanism well – claudioz Sep 25 '17 at 08:32
  • There's plenty of samples in the duplicate. – deceze Sep 25 '17 at 08:34
  • ok, so I follow the callback mechanism and not the promise mechanism? – claudioz Sep 25 '17 at 08:38
  • You can give me a valid example in the duplicate? Sorry but they are too many and I have really little time – claudioz Sep 25 '17 at 08:45
  • Mostly as I apply it all without the use of ajax? – claudioz Sep 25 '17 at 09:06

0 Answers0