Created
July 21, 2021 22:48
-
-
Save nhuxhr/043b8148a65ff6a77275c61946b226a2 to your computer and use it in GitHub Desktop.
Synchronous fetch using async/await in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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