“Axios async Get” Ответ

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();
Daniel Jay

Axios async ожидает

/**
 * 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
        // #
    }
}
Innocent Ibis

Ответы похожие на “Axios async Get”

Вопросы похожие на “Axios async Get”

Больше похожих ответов на “Axios async Get” по JavaScript

Смотреть популярные ответы по языку

Смотреть другие языки программирования