Skip to content

Instantly share code, notes, and snippets.

@sakshstore
Last active August 10, 2023 03:38
Show Gist options
  • Save sakshstore/303811e9c4f56eac66a440923aeed77c to your computer and use it in GitHub Desktop.
Save sakshstore/303811e9c4f56eac66a440923aeed77c to your computer and use it in GitHub Desktop.
async/await is a modern approach to handle asynchronous code, making it look more synchronous and easier to read.
// Longhand
function fetchData() {
return fetch('https://api.example.com/data')
.then((response) => response.json())
.then((data) => {
console.log(data);
})
.catch((error) => {
console.error(error);
});
}
// Shorthand
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment