Answers for "axios async await"

14

async axios call

async getUserAsync(name) {
    try{
      let response = await axios({
          method: 'get',
          url: `https://api.github.com/users/${name}`,
          json: true
      });
      return response;
    } catch(err){
        console.error(err);
    }
}
Posted by: Guest on March-04-2020
1

axios async await

/**
 * Axios : Async/Await
 * Put within method
 */
function fetchSampleData() {
    let method = 'get' // ex. get | post | put | delete , etc
    return axios[method](url,params)
        .then((response) => {
            // success
            //-> save response to state, notification

            return true // pass to finish
        })
        .catch((error) => {
            // failed
            //-> prepare, notify, handle error

            return false // pass to finish
        })
        .then((resultBoolean) => {
            // do something after success or error

            return resultBoolean // for await purpose
        });
}

// Implementation
async function fetchResult() {
    let success = await fetchSampleData()
    if (success) {
        // handle success 
        // #
    } else {
        // handle error
        // #
    }
}
Posted by: Guest on September-21-2021
-1

async await with axios

async await try catch
Posted by: Guest on September-24-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language