NodeJS как мне выполнить сначала fetch и после получение ответа остальной код?

let news;
fetch('http://localhost:80/read', {
  method: 'post'
})
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    // console.log(data);
    news = data; //Здесь получаю данные
    console.log(news);
  });

Сейчас у меня все приложение ломается т.к news изначально пустой.


Ответы (1 шт):

Автор решения: Евгений
async function fetchData(url){
    let news;
    try{
        news = await fetch(`${url}`, {
          method: 'post'
        })
          .then(response => {return response.json()});
    } catch (err){
        console.error(err)
    }
    console.log(news)
    // какой-то код
}
fetchData('http://www.json-generator.com/api/json/get/ceNgEyGaNu?indent=2');
→ Ссылка