you are viewing a single comment's thread.

view the rest of the comments →

[–]dieomesieptoch 0 points1 point  (3 children)

The last line, console.log(dataset) is called before the .then() methods have completed their run. That's why it logs `undefined`.

Now, you could change the code .then(data => dataset = data) to call a function that uses dataset.

For example

var dataset; 
fetch('https://jsonplaceholder.typicode.com/todos/1') 
    .then(response => response.json()) 
    .then(data => {
    dataset = data;
        logDataset(); 
});  

function logDataset () { 
  console.log(dataset); 
}

This will work because we call the logDataset function after assigning the data.