fetch()
метод возвращает Promise
, который преобразуется в Response
для этого запроса, независимо от того, был он успешным или нет .
receiveProfile
функция возвращает resultObject
(который изначально не определен) до завершения блока fetch()
. Следует дождаться разрешения обещания.
У вас есть 2 варианта:
1. С async / await
async receiveProfile(username) {
const url = `https://www.instagram.com/${username}/?__a=1`;
const response = await fetch(url);
if (response.status !== 200) {
throw new Error(response.status);
}
const data = await response.json();
return new UserProfile(
data.graphql.user.full_name,
data.graphql.user.biography,
data.graphql.user.profile_pic_url,
data.graphql.user.external_url,
data.graphql.user.edge_owner_to_timeline_media.edges,
data.graphql.user.edge_followed_by,
data.graphql.user.edge_follow
);
}
Демо:
// Dummy class
class UserProfile {
constructor(full_name, biography, profile_pic_url, external_url, edges, edge_followed_by, edge_follow) {
this.full_name = full_name;
this.biography = biography;
this.profile_pic_url = profile_pic_url;
this.external_url = external_url;
this.edges = edges;
this.edge_followed_by = edge_followed_by;
this.edge_follow = edge_follow;
}
}
async function receiveProfile(username) {
const url = `https://www.instagram.com/${username}/?__a=1`;
const response = await fetch(url);
if (response.status !== 200) {
throw new Error(response.status);
}
const data = await response.json();
return new UserProfile(
data.graphql.user.full_name,
data.graphql.user.biography,
data.graphql.user.profile_pic_url,
data.graphql.user.external_url,
data.graphql.user.edge_owner_to_timeline_media.edges,
data.graphql.user.edge_followed_by,
data.graphql.user.edge_follow
);
}
receiveProfile('instagram').then(function(user) {
console.log(user);
});
2. Без асинхронности / ожидания
receiveProfile(username) {
const url = `https://www.instagram.com/${username}/?__a=1`;
return fetch(url).then(function(response) {
return response.json();
}).then(function(data) {
return new UserProfile(
data.graphql.user.full_name,
data.graphql.user.biography,
data.graphql.user.profile_pic_url,
data.graphql.user.external_url,
data.graphql.user.edge_owner_to_timeline_media.edges,
data.graphql.user.edge_followed_by,
data.graphql.user.edge_follow
);
}).catch(function(error) {
console.error('Error: ', error);
});
}
Демо:
// Dummy class
class UserProfile {
constructor(full_name, biography, profile_pic_url, external_url, edges, edge_followed_by, edge_follow) {
this.full_name = full_name;
this.biography = biography;
this.profile_pic_url = profile_pic_url;
this.external_url = external_url;
this.edges = edges;
this.edge_followed_by = edge_followed_by;
this.edge_follow = edge_follow;
}
}
function receiveProfile(username) {
const url = `https://www.instagram.com/${username}/?__a=1`;
return fetch(url).then(function (response) {
return response.json();
}).then(function (data) {
return new UserProfile(
data.graphql.user.full_name,
data.graphql.user.biography,
data.graphql.user.profile_pic_url,
data.graphql.user.external_url,
data.graphql.user.edge_owner_to_timeline_media.edges,
data.graphql.user.edge_followed_by,
data.graphql.user.edge_follow
);
}).catch(function(error) {
console.error('Error: ', error);
});
}
receiveProfile('instagram').then(function (user) {
console.log(user);
});