zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    [转载]PhotoShop性能优化
    SVN常用命令
    [转载]SVN使用教程
    MyEclipse Java Build Path详解
    MyEclipse安装后需要进行的配置
    c#中base64加密解密
    C# MD5 加密
    C# http Post 方法
    EPF与Myeclipse 增强代码自动智能提示
    汉字代码手册
  • 原文地址:https://www.cnblogs.com/sexintercourse/p/12267236.html
Copyright © 2011-2022 走看看