简体   繁体   English

javaScript中的异步和同步

[英]Asynchronous and Synchronous in javaScript

I have to use the SP.RequestExecutor.js library. 我必须使用SP.RequestExecutor.js库。 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). 经过深入搜索,我发现了awaitasync方法,但它们与Internet Explorer(IE> 9)不兼容。 How I can make convert the async functions to sync and be compatible on IE>9 and Chrome? 如何使异步功能转换为同步并在IE> 9和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: 我需要输出ass如下:

end1 end2 end3 end4. end1 end2 end3 end4。

The above code is running in chrome but on IE refuse the await and async . 上面的代码在chrome中运行,但在IE上拒绝awaitasync

In order for await to work, executorRun needs to return a Promise . 为了await工作, executorRun需要返回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 . 要在IE9中使用async / await ,您可以使用Babel编译代码。

You could also just use the Promise directly without the syntactic sugar of async / await : 您也可以直接使用Promise而不使用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. IE本身不支持Promises,但是可以很容易地用许多Promise库中的任何一个对其进行轮询。

If you don't want to use Promises, you could always just modify excecutorRun to accept a callback: 如果您不想使用Promises,则总是可以修改excecutorRun以接受回调:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM