Last active
June 30, 2018 03:20
-
-
Save samzhao/9a2374ac1710d1acab61345d2f8b760b to your computer and use it in GitHub Desktop.
Simple Async/await Example
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
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