So I want to refactor my code from jquery to just javascript ES6, I think it will be cleaner if it implemented that way. But I'm not sure how to implement it Here is the jquery code :
$.post({
url: /* my url */
data: { /* my data*/ }
beforeSend: () => {
//do something
},
success: (res) => {
//do something
},
error: (err) => {
console.error(err)
},
complete: () => {
//do something
}
})
How to refactor to ES6 fetch to look something like this :
fetch(url, {
method: 'POST',
body: /* my data */
})
// beforeSend code
.then((res) => res.json())
.then((data) => {
//do something
})
// complete code
.catch((err) => console.log(err));
I will be very grateful if you can write the code with async await, but .then() .catch() is appreciated regardless. Thanks in advance