I am new to JavaScript. In the code below, when I log in, I try to get the user info from database and store in user object. However, when I check the user in the commandManager scope, it turns null. How can I make user not null?
Here is the code:
function LogInCommand(username, password) {
return new Command(function (user) {
api
.logIn(username, password)
.then(function (data) {
user = data;
console.log(user);
})
.catch(function (error) {
console.log(error);
});
});
}
function createCommandManager() {
var user = null;
return {
execute: function (command, ...args) {
command.execute(user, ...args);
},
user: user
};
}
var commandManager = createCommandManager();
commandManager.execute(new LogInCommand("[email protected]", "123456"));
setTimeout(function() {
console.log(commandManager.user);
}, 10000);
Here is the result:
{age: 21, connection: null, email: "[email protected]", name: "andy", pendingConnection: null, …}
null
Thanks all of you for your helps!
commandManageris undefiend at the linecommandManager.execute(new LogInCommand("[email protected]", "123456"));