0

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.

4
  • Could you please show your code? Mar 14, 2017 at 7:13
  • @Muhammad Qasim plz see the update
    – myomyo
    Mar 14, 2017 at 7:28
  • those are badly formatted code
    – syarul
    Mar 14, 2017 at 8:06
  • const won't work in IE9 either. You could use var there instead. Mar 14, 2017 at 8:17

1 Answer 1

0

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();
1
  • Thank you first choice what I need using babel can be compatible by all browsers
    – myomyo
    Mar 14, 2017 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.