Не могу получить данные из fetch в переменную

В общем вот такая проблема, получаю через fetch объект, в консоль свойство объекта могу показать, а присвоить его переменной не могу. Помогите пжл присвоить данные переменной.

let age = 0

fetch('https://randomuser.me/api/?results=10')
    .then((resp) => resp.json())
    .then(function (data) {
      console.log(data.results[0].dob.age+" years"); //displays the correct age in the console
      age = data.results[0].dob.age;
      return age;
    }); 

console.log(age+" age console");
// why doesn't fetch assign age variable data? how to assign data.results[0].dob.age to the variable age?

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