DEV Community

Discussion on: Beginners Guide To Fetching Data With (AJAX, Fetch API & Async/Await)

Collapse
 
pierrejoubert73 profile image
Pierre Joubert

Do you not like the syntax? Or do you not want to deal with promises?

Collapse
 
itachiuchiha profile image
Itachi Uchiha

I just wonder about assign result to a variable. I don't wan't to use then all time. For example:

const myResults = fetch('site.com')
   .then(resp => resp.json())
   .then(obj => obj)

console.log(myResults)
// { name: 'John', surname: 'Doe' }

Thread Thread
 
pierrejoubert73 profile image
Pierre Joubert • Edited

I just did this and it seemed to work:

var foo = null;
fetch('https://jsonplaceholder.typicode.com/posts/1')
   .then(resp => resp.json())
   .then(obj => foo = obj)

Then foo is accessible object like: foo.title
"sunt aut facere repellat provident occaecati excepturi optio reprehenderit"

Thread Thread
 
itachiuchiha profile image
Itachi Uchiha

Thanks a lot. That's exactly what I want.