0

I have the following structure of 2 functions -

function myfunc(param1, param2){
         //some stuff happens 
         //some stuff happens
           let request = new XMLHttpRequest();
    request.open("POST",documentumUploadAPI);

    request.setRequestHeader('Authorization', 'Bearer ' + token);
    request.setRequestHeader('Content-Type', 'multipart/form-data');
    request.send(formData);
    var flag = false;
    var flag1 = 'not successful';
  
     request.onreadystatechange = function() {
        if (request.readyState === 4) {

        flag = request.responseText.includes('"success" : true');
        if(flag){
          flag1 = 'successful';

              }

          }

      }

}

Now i want to return the value of flag1 and use it but since the scope of the variable is limited to the function only, i cannot get the value of flag1 as 'successful'. Is there any way to return the value of flag1 being changed in the onreadystatechange function?

1 Answers1

0

You have many solutions for this problem, but first, you need to understand the problem and it's about asynchronous in js.

For an explanation of the problem + a couple of answers - return value from async call

The best way for your problem I think is to use a callback that you pass to the function to run the rest of your code and use that var.