How to make async function to wait to complete its execution in native javascript

Viewed 32

enter image description here

Above is the code, for http POST call. I am facing the issue wherein the code flow returns back and check the onreadystatechange function, it will go to the else section, the code flow doesnot go inside the if condition. Any help is much appreciated, thanks!.

Edit: Posted the code snippet, regret for posting it as screenshot above.

var xhr = new XMLHttpRequest();
var urlToPost = "<%=WriteAttachmentUploadURL %>"
xhr.open('POST', urlToPost,true);
xhr.onreadystatechange = function() {
if(xhr.readyState === XMLHttpRequest.DONE) {
console.log(xhr.responseText);
return xhr.responseText;
} else {
console.log('upload failed');

}

}
0 Answers
Related