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", "name": "nuxt-custom-routes",
"description": "",
"dependencies": { "dependencies": {
"axios": "^0.15.2", "axios": "latest",
"nuxt": "latest" "nuxt": "latest"
}, },
"scripts": { "scripts": {

View File

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

View File

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