Created
July 19, 2018 14:53
-
-
Save xianminx/191836ec069780f2ad282f3798b0252c to your computer and use it in GitHub Desktop.
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
function hello_axios() { | |
const axios = require('axios'); | |
axios.get('https://cityapi.opentown.cn/events/5b3a2aed66db7502c0a2760x').then(function (response) { | |
console.log(response); | |
}).catch(function (error) { | |
console.log(error); | |
}).then(function () { | |
console.log('always executed') | |
}); | |
// Make a request for a user with a given ID | |
axios.get('/user?ID=12345') | |
.then(function (response) { | |
// handle success | |
console.log(response); | |
}) | |
.catch(function (error) { | |
// handle error | |
console.log(error); | |
}) | |
.then(function () { | |
// always executed | |
}); | |
// Optionally the request above could also be done as | |
axios.get('/user', { | |
params: { | |
ID: 12345 | |
} | |
}) | |
.then(function (response) { | |
console.log(response); | |
}) | |
.catch(function (error) { | |
console.log(error); | |
}) | |
.then(function () { | |
// always executed | |
}); | |
// Want to use async/await? Add the `async` keyword to your outer function/method. | |
async function getUser() { | |
try { | |
const response = await axios.get('/user?ID=12345'); | |
console.log(response); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
function getUserAccount() { | |
return axios.get('/user/12345'); | |
} | |
function getUserPermissions() { | |
return axios.get('/user/12345/permissions'); | |
} | |
axios.all([getUserAccount(), getUserPermissions()]) | |
.then(axios.spread(function (acct, perms) { | |
// Both requests are now complete | |
})); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment