DEV Community

Discussion on: "Synchronous" fetch with async/await

Collapse
 
gtnovelt profile image
gt-novelt

I find more readable :

const json = await fetch('https://api.com/values/1')
                .then(response => response.json());
console.log(json);
Enter fullscreen mode Exit fullscreen mode

Fetch returns a promise, no need to wrap this in an async function.

Collapse
 
ewirth profile image
Ervin Wirth

Not working...
SyntaxError: await is only valid in async functions and async generators

Collapse
 
totalamd profile image
totalamd • Edited

you need to wrap everything in async func