1async getUserAsync(name) {
2 try{
3 let response = await axios({
4 method: 'get',
5 url: `https://api.github.com/users/${name}`,
6 json: true
7 });
8 return response;
9 } catch(err){
10 console.error(err);
11 }
12}
13
14
1/**
2 * Axios : Async/Await
3 * Put within method
4 */
5function fetchSampleData() {
6 let method = 'get' // ex. get | post | put | delete , etc
7 return axios[method](url,params)
8 .then((response) => {
9 // success
10 //-> save response to state, notification
11
12 return true // pass to finish
13 })
14 .catch((error) => {
15 // failed
16 //-> prepare, notify, handle error
17
18 return false // pass to finish
19 })
20 .then((resultBoolean) => {
21 // do something after success or error
22
23 return resultBoolean // for await purpose
24 });
25}
26
27// Implementation
28async function fetchResult() {
29 let success = await fetchSampleData()
30 if (success) {
31 // handle success
32 // #
33 } else {
34 // handle error
35 // #
36 }
37}