-5

So, I'm new to using promise object... I'm trying to make a Facebook instant game which uses the function FBInstant.player.getDataAsync().then(). This function returns a promise and I have no clue how to use the data returned other than with console.log(). If I try to insert the data to a defined variable it becomes unidentified.

Example:

User = [];
FBInstant.player.getDataAsync(['name']).then(function(data) {
    User['name'] = data['name'];
});

console.log(User['name']) // return unidentified.

Sorry for my poor explanation I'm new to this and I'm just trying to accomplish a simple task.

user1181378
  • 128
  • 1
  • 11
  • 1
    because `console.log(User['name'])` is read BEFORE `User['name'] = data['name'];` runs. That is how Asynchronous calls work. You need to do the logic inside of that then() or call the next step. – epascarello Nov 15 '21 at 14:03

1 Answers1

0
User = [];
FBInstant.player.getDataAsync(['name']).then(function(data) {
    User['name'] = data['name'];
    console.log(User['name'])
});

This should give you a response

sc0rp1on
  • 348
  • 1
  • 15