Skip to content

Instantly share code, notes, and snippets.

@samzhao
Last active June 30, 2018 03:20
Show Gist options
  • Save samzhao/9a2374ac1710d1acab61345d2f8b760b to your computer and use it in GitHub Desktop.
Save samzhao/9a2374ac1710d1acab61345d2f8b760b to your computer and use it in GitHub Desktop.
Simple Async/await Example
import axios from 'axios';
const API_URL = "https://jsonplaceholder.typicode.com/posts/1";
// Before async/await
const logPostId = () => {
axios.get(API_URL)
.then(resp => {
const { data } = resp;
const { id } = data;
console.log('Post Id:', id);
});
}
logPostId(); // Post Id: 1
// After async/await
const logPostId = async () => {
const { data } = await axios.get(API_URL);
const { id } = data;
console.log('Post Id:', id);
}
logPostId(); // Post Id: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment