Use async/await

This commit is contained in:
Sébastien Chopin 2017-07-08 18:52:29 +02:00
parent a39b9fe13b
commit eaf362910f
3 changed files with 10 additions and 12 deletions

View File

@ -1,8 +1,7 @@
{
"name": "nuxt-custom-routes",
"description": "",
"dependencies": {
"axios": "^0.15.2",
"axios": "latest",
"nuxt": "latest"
},
"scripts": {

View File

@ -13,11 +13,9 @@
import axios from 'axios'
export default {
asyncData () {
return axios.get('https://jsonplaceholder.typicode.com/users')
.then((res) => {
return { users: res.data }
})
async asyncData () {
const { data } = await axios.get('https://jsonplaceholder.typicode.com/users')
return { users: data }
}
}
</script>

View File

@ -14,12 +14,13 @@ export default {
validate ({ params }) {
return !isNaN(+params.id)
},
asyncData ({ params, error }) {
return axios.get(`https://jsonplaceholder.typicode.com/users/${+params.id}`)
.then((res) => res.data)
.catch(() => {
async asyncData ({ params, error }) {
try {
const { data } = await axios.get(`https://jsonplaceholder.typicode.com/users/${+params.id}`)
return data
} catch (e) {
error({ message: 'User not found', statusCode: 404 })
})
}
}
}
</script>