前端神器Promise的用法介绍

说起Promise相信大家都知道,它有一个很好的用法,就是可以在ajax请求之外对数据进行操作,避免了多层ajax的嵌套,可以使代码阅读起来更直观,下面是Promise的用法。

queryJydj(accom1);
function queryJydj(acc0m1) {
        //console.log(acc0m1);
        var ACC0M1 = { "ACC0M1": acc0m1 };
        var result;//用于接收拿到的数据
        $.ajax({
            type: "POST",
            data: AAC0M1,
            url: "/HrmisService/QueryCc03ServiceProxy/query",
            success: function (response){
                new Promise(function (reslove,reject){
                    //成功后,且有数据
                    if (response.IsOK) {
                        //存储成功拿到的数据
                        result = response;
                        reslove();
                    } else {
                        //存储拿到的失败的数据
                        result = response;
                        reject();
                    };
                })
            }
        }).then(function () {
            //这里就可以拿到ajax请求回的数据在外部进行操作,避免了多层ajax的嵌套。
            console.log(result);
        })
    };

你可能感兴趣的:(web前端开发,promax处理教程)