Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

axios async get

const axios = require('axios');

const sendGetRequest = async () => {
    try {
        const resp = await axios.get('https://jsonplaceholder.typicode.com/posts');
        console.log(resp.data);
    } catch (err) {
        // Handle Error Here
        console.error(err);
    }
};

sendGetRequest();
Comment

axios api get request async

async function getUserData() {
	try {
		const response = await axios.get("/user_login/john1904");
		console.log(response);
	}
	catch (error) {
		console.log(error);
	}
}
Comment

axios async await

async fetchCatFacts() {
    await axios.get("//localhost:8082/api_v1/orders", {})
        .then((response) => {

          this.catFacts = response.data.data;
          console.log("resp", response.data);
        });
 Run code snippet
Comment

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
        // #
    }
}
Comment

Javascript axios get async await

const axios = require('axios');

async function doGetRequest() {

  let res = await axios.get('http://webcode.me');

  let data = res.data;
  console.log(data);
}

doGetRequest();
Comment

PREVIOUS NEXT
Code Example
Javascript :: update column with find sequelize 
Javascript :: fetch api example 
Javascript :: what is node 
Javascript :: replace element javascript 
Javascript :: Plugin "react" was conflicted between "package.json » eslint-config-react-app 
Javascript :: angular mat side nav 
Javascript :: else in javascript 
Javascript :: loadsh debounce 
Javascript :: Expected an assignment or function call and instead saw an expression 
Javascript :: javascript reverse array and separate by spaces 
Javascript :: replace all swear words using bookmarklet 
Javascript :: django send and receive image data to react 
Javascript :: redux if already exist item dont add to array 
Javascript :: rect js 
Javascript :: N-dim object support meanigh 
Javascript :: moment_timezone_1.default(...).tz(...).format is not a function 
Javascript :: react state deconstructed 
Javascript :: vs 2019 how to publish angular environment prod 
Javascript :: bcrypt always return faslse in node js 
Javascript :: cheatsheet addeventlistener 
Javascript :: react algoliasearch 
Javascript :: styleurls component angular node_modules 
Javascript :: how to add theme attribute to :root 
Javascript :: nodejs share session 
Javascript :: passportjs mac req.user not saved 
Javascript :: get value in maps loop using enzym 
Javascript :: jwtdecode has no call signatures angular 
Javascript :: how to fix a vulnerability in expo 
Javascript :: mongodb js get id of inserted 
Javascript :: lookupedit devexpress get specific row 
ADD CONTENT
Topic
Content
Source link
Name
6+5 =