Answer the question
In order to leave comments, you need to log in
How to do fetch correctly in my case (Promise pending)?
Good evening.
There is an array, using map I draw each element of the array.
There is a question on one moment connected with fetch.
Here I am using the function<img src={this.getPromoUrl(tweet.id, "image")} />
getPromoUrl = (id, param) => {
const fetchData = this.getPromise(id, param);
console.log(fetchData);
return fetchData;
};
getPromise = async (id, param) => {
let data;
try {
const response = await fetch(`https://someurl/api/v1/statuses/${id}/card?access_token=${process.env.REACT_APP_KEY}`);
const json = await response.json();
data = json[param];
} catch (e) {
console.log(e);
}
return data;
};
// В консоле все ок, строка
console.log(fetchData);
Answer the question
In order to leave comments, you need to log in
getPromoUrl = async (id, param) => {
const data = await this.getPromise(id, param);
console.log(data);
this.setState({ data });
};
getPromoUrl = (id, param) => {
this.getPromise(id, param).then(data => {
console.log(data);
this.setState({ data });
});
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question