axios how to return promise code example

Example 1: use axios with Promise.all

const axios = require('axios');

const someFunction = () => {
    return new Promise(resolve => {
        setTimeout(() => resolve('222'), 100)
    })
}

const requestsData = ['https://httpstat.us/200', 'https://httpstat.us/205', 'https://httpstat.us/306']
const requestArr = requestsData.map(async data => {
    let waitForThisData = await someFunction(data);
    return axios.post(data)
            .then(response => {})
            .catch(error => console.log(error.toString()))
});

Promise.all(requestArr).then(() => {
    console.log('resolved promise.all')
})

Example 2: what is axios .finally on promise

//The finally() method can be useful if you want to do some processing or cleanup once the promise is settled, regardless of its outcome.

//So if you want to setloading to false regardless of error or success, do this

axios
      .get('/products', { params: params })
      .then((response) => {
        if (isMountedRef.current) {
          setProducts(response.data.data);
          setMeta(response.data.meta);
        }
      })
      .finally(() => {
        setLoading(false);
      });