Q:

Asynchronous and Synchronous in javaScript

I have to use the SP.RequestExecutor.js library. The problem is I need to run the async function in sync behavior. After deep search I found await and async methods but they are not compatible with Internet Explorer(IE>9). How I can make convert the async functions to sync and be compatible on IE>9 and Chrome?
function executorRun() {
  console.log('end2');
  var executor = new SP.RequestExecutor('path'); 
  var result=[];
  executor.executeAsync({    
      url: 'URL',
      method: "POST",
      headers: {
          "accept": "application/json;odata=verbose",
          "content-type": "application/json;odata=verbose",      
      },
      data: JSON.stringify(requestData),
      success: function (data) {
        console.log('end3')
        console.log(data);//Debug statement 
        //Handle data and store in result       
      },       
      error: function (error) {
        console.log(error);
      }
   });
  return result;
}

async function test () { 
  console.log('end1');
  const data = await executorRun();
  console.log('end4');
}

test();

I need the output ass follows:

end1
  end2
  end3
  end4.

The above code is running in chrome but on IE refuse the await and async.

A:

In order for await to work, executorRun needs to return a Promise.
function executorRun() {
  console.log('end2');
  return new Promise(function (resolve, reject) {
    var executor = new SP.RequestExecutor('path'); 

    executor.executeAsync({    
        url: 'URL',
        method: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",      
        },
        data: JSON.stringify(requestData),
        success: function (data) {
          console.log('end3');
          console.log(data);//Debug statement 
          //Handle data and store in result       
          resolve(data);
        },       
        error: function (error) {
          reject(error);
          console.log(error);
        }
     });
  });
}

async function test () { 
  console.log('end1');
  const data = await executorRun();
  console.log('end4');
}

test();

To use async/await in IE9, you coudl transpile your code with Babel.
You could also just use the Promise directly without the syntactic sugar of async/await:
function test () { 
  console.log('end1');
  executorRun().then(function (data) {
    console.log('end4');
  });
}

test();

IE doesn't support Promises natively, but they can easily be pollyfilled with any of the many Promise libraries out there.
If you don't want to use Promises, you could always just modify excecutorRun to accept a callback:
function executorRun(callback) {
  console.log('end2');
    var executor = new SP.RequestExecutor('path'); 

    executor.executeAsync({    
        url: 'URL',
        method: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",      
        },
        data: JSON.stringify(requestData),
        success: function (data) {
          console.log('end3');
          console.log(data);//Debug statement 
          //Handle data and store in result       
          callback(data);
        },       
        error: function (error) {
          console.log(error);
        }
     });
}

function test () { 
  console.log('end1');

  executorRun(function (data) {
    console.log('end4');
    console.log(data);
  });
}

test();

