Answer the question
In order to leave comments, you need to log in
`
How to spell Promise correctly?
change has a function
export const getNews = async () => {
let news;
try {
fetch(`https://newsapi.org/v2/top-headlines?country=us&apiKey='''''''''`)
.then(res => res.json())
.then(res => {
news = res;
})
} catch(e){
throw e;
}
return news;
};
getNews()
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
Answer the question
In order to leave comments, you need to log in
export const getNews = async () => {
let news;
try {
news = await fetch(`https://newsapi.org/v2/top-headlines?country=us&apiKey='''''''''`).then(res => res.json());
} catch(e){
throw e;
}
return news;
};
You don't need to use try...catch, you use promises, and they have their own implementation of error handling.
export const getNews = async () => {
return fetch(`https://newsapi.org/v2/top-headlines?country=us&apiKey='''''''''`)
.then(res => res.json())
.then(res => {
return res;
});
};
getNews()
.then(news => {
console.log(news);
})
.catch(error => {
console.log(error);
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question