During the process of making a javascript AJAX request, I initially utilized the traditional callback
approach to call the callback function within the onreadystatechange
and retrieve all the values of readyState
.
However, upon switching my callback
functions to promises, I observed that resolving in the onreadystatechange
only returned the initial readyState
value which was 2, rather than providing all values including 2, 3, and 4.
_.request = async (headers, path, method, queryObj, payload) => {
// Promise implementation logic here...
}
$(document).on('ready', async (e) => {
const data = await _.request(undefined, '/views/getarticle', 'get', undefined, undefined);
console.log(data); // readyState: 2
});
I had anticipated receiving all the readyState
values. If my current strategy isn't effective, is there an alternative method to achieve this without relying on callback
functions?