Skip to content

Instantly share code, notes, and snippets.

@i-mighty
Created December 2, 2021 06:26
Show Gist options
  • Save i-mighty/1812210f21370b5b93c36f0980d4aa7c to your computer and use it in GitHub Desktop.
Save i-mighty/1812210f21370b5b93c36f0980d4aa7c to your computer and use it in GitHub Desktop.
Axios snippets
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