Can you please explain me why I can't access the testObj
's key outside the .then() body?
Console.log results are shown in the comments.
//my object
let testObj = {
title: "",
};
//my array
let testAr = [];
fetch("https://jsonplaceholder.typicode.com/todos/1")
.then((response) => response.json())
.then((data) => {
testObj.title = data.title;
testAr.push(data.title);
console.log(testObj);
//object log #1 - contents good
//
//{title: "delectus aut autem"}
// title: "delectus aut autem"
// __proto__: Object
console.log(testAr);
//array log #1 - contents good
//
//["delectus aut autem"]
// 0: "delectus aut autem"
// length: 1
// __proto__: Array(0)
});
console.log(testObj);
//object log #2 - cant access contents although they are displayed in log
//
// {title: ""}
// title: "delectus aut autem"
// __proto__: Object
console.log(testAr);
//array log #2 - same as above
//
// []
// 0: "delectus aut autem"
// length: 1
// __proto__: Array(0)