Hello,
Another JS beginner here.I have a couple of questions.
#1 Wondering how the arrow function accesses the two elements of the array in getUsers() as it is not clear to me.
#2 In function findUser the arrow function that is passed has a argument of users. It is used as an
array variable in getUsers but how does one know that seeing there isn't a declaration or definition of it anywhere.
I did try to directly substitute the arrow function in getUsers() instead of passing it to try to figure out how the whole
thing works but could not clear the errors that resulted.
Another JS beginner here.I have a couple of questions.
#1 Wondering how the arrow function accesses the two elements of the array in getUsers() as it is not clear to me.
#2 In function findUser the arrow function that is passed has a argument of users. It is used as an
array variable in getUsers but how does one know that seeing there isn't a declaration or definition of it anywhere.
I did try to directly substitute the arrow function in getUsers() instead of passing it to try to figure out how the whole
thing works but could not clear the errors that resulted.
Code:
function getUsers(callback) {
setTimeout(() => {
callback([
{ username: 'john', email: '[email protected]' },
{ username: 'jane', email: '[email protected]' },
]);
}, 1000);
}
function findUser(username, callback1) {
getUsers((users) => {
const user = users.find((user) => user.username === username);
callback1(user);
});
}
findUser('jane', console.log); //console.log is a function that can take a parmteter