Created
December 2, 2021 06:26
-
-
Save i-mighty/1812210f21370b5b93c36f0980d4aa7c to your computer and use it in GitHub Desktop.
Axios snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from "axios"; | |
const getRequest = async (url) => { | |
try { | |
const response = await axios.get(url); | |
if (response.status === 200) { | |
return response.data; | |
} else throw new Error("Any error text"); | |
} catch (error) { | |
console.log(error); | |
// any other custom error reporting logic | |
} | |
} | |
const postRequest = async (url, data) => { | |
try { | |
const response = await axios.post(url, data) | |
if (response.status === 200) { | |
return response.data; | |
}else throw new Error("Any error text"); | |
} catch (error) { | |
console.log(error); | |
// any other custom error reporting logic | |
} | |
} | |
const getRequestThen = (url, callback) => { | |
axios.get(url).then(res => { | |
if (res.status === 200) { | |
callback(res.data); // or any other custom logic | |
} else throw new Error("Any error text"); | |
}).catch(error => { | |
console.log(error); | |
// any other custom error reporting logic | |
}); | |
} | |
const postRequestThen = (url, data, callback) => { | |
axios.post(url, data).then(res => { | |
if (res.status === 200) { | |
callback(res.data); // or any other custom logic | |
} else throw new Error("Any error text"); | |
}).catch(error => { | |
console.log(error); | |
// any other custom error reporting logic | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment