Promise的金字教程

1、常规的调用方法

<script>
          new Promise(function (resolve, reject) {
            $.ajax({
                url: "GetData",
                dataType: "JSON",
                type: "POST",
                success: function (data) {
                    resolve(data)
                }
            })
          }).then(({ responseData }) => { console.log(responseData) });
</script>

2、调用返回值的方式。

<script>
    function p1() {
        return new Promise(function (resolve, reject) {
            $.ajax({
                url: "GetData",
                dataType: "JSON",
                type: "POST",
                success: function (data) {
                    resolve(data)
                }
            })
        })
    }
    $(function () {
        p1().then(({ responseData }) => { console.log(responseData) });
    })
 
  
</script>

3、和await,async进行结合的教程。

<script>
    function p1() {
        return new Promise((resolve, reject)=>{
            $.ajax({
                url: "GetData",
                dataType: "JSON",
                type: "POST",
                success: function (data) {
                    resolve(data.responseData)
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    reject("网络错误")
                }
            })
        })
    }

    async function GetData() {
        let res = await p1();
        return res;
    }

    async function GetData1() {
        let res = await GetData();
        console.log(res);
    }

    



    $(function () {
        GetData1();
    })


</script>

 

posted on 2020-02-05 23:16  漫思  阅读(151)  评论(0编辑  收藏  举报

导航