es6——async&await去除回调嵌套

<script src="https://code.jquery.com/jquery-3.1.1.min.js">script>
<script type="text/javascript">
async function ajax(url) {
    return new Promise(function (resolve, reject) {
        let ajaxSetting = {
            url: url,
            dataType: 'text',
            success: function (response) {
              resolve(response);
            },
            error: function (e) {
                console.log(e);
                reject("请求失败");
            }
        }
        $.ajax(ajaxSetting);

    });
}
async function run() {
  try{
    let response1 = await ajax("text1.json");
    //console.log(response1);
    let response2 = await ajax("text2.json");
    console.log(response1);
    console.log(response2);
  }catch(e){
    console.log(e);
  }
}
$(function(){
  run();
});


script>

你可能感兴趣的:(HTML5+CSS3)