Skip to content

Instantly share code, notes, and snippets.

@nhuxhr
Created July 21, 2021 22:48
Show Gist options
  • Save nhuxhr/043b8148a65ff6a77275c61946b226a2 to your computer and use it in GitHub Desktop.
Save nhuxhr/043b8148a65ff6a77275c61946b226a2 to your computer and use it in GitHub Desktop.
Synchronous fetch using async/await in JavaScript
/**
* Synchronous fetch using async/await in JavaScript
* @author JSX Clan <jsxclan.dev@gmail.com>
*/
// Usual way
const data = fetch('URL').then(res => res.json()).then(json => console.log(json));
// Using await
const data = await fetch('URL').then(res => res.json());
// Shorter syntax
const data = await (await fetch('URL')).json();
// Happy Coding!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment