I am trying to return an object from the function called getByUsername()
. The correct object appears in the then()
block. i'm unable to return that object from the outer function which returns undefined.
const url = 'https://jsonplaceholder.typicode.com/users/';
function get(url) {
return fetch(url)
.then(response => response.json());
}
const users = get(url)
.then(users => users);
function getByUsername(x) {
return users.then((user) => {
return user.find((user) => {
return user.username === x;
});
})
.then((user) => {
console.log(user); // Correct object
return user;
});
}
getByUsername('Kamren');