Well, you have 2 ways to do that:
The first one is using your way, with callbacks. You just need to store the request in a variable, and then use the "then/catch" on this one.
function update(
url,
usepost = true,
arg = {},
callback = () => {},
errorcb = () => {}
) {
let request;
if (usepost) {
request = axios.post("https://example.com/" + url, arg)
} else {
request = axios.get("example.com/" + url, { params: arg })
}
request.then(response => {
// do stuff
callback();
})
.catch(error => {
// do error stuff
errorcb();
});
}
The 2nd way, a better way in my opinion, is to simply make your function async and return the request (which is a promise). Using this way, you can easily manage async stuff using promises.
async function update(
url,
usepost = true,
arg = {}
) {
if (usepost) {
return axios.post("https://example.com/" + url, arg)
} else {
return axios.get("example.com/" + url, { params: arg })
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…