jq和Promise和$.ajax超时

function getData() {
    return new Promise((resolve, reject) => {
        //请求接口
        $.ajax({
            type: 'POST',
            url: "/pad/overseas/GetNeedWoodPackageList",
            data: {
                PageIndex: pg,
                PageSize: 10000,
                RecordCount:0,
            },
            timeout: 12000,
            async: true,//设置超时只能为异步,默认异步
            dataType: "json",
            success: function (msg) {
                if (msg.status.code == "1") {
                    //返回数据
                    resolve(msg.row_data.record)
                }
                else {
                    reject(msg.status.msg)//会返到alertWindows("提示", err, 2);
                }
            },
            complete: function(XMLHttpRequest, textStatus) {//无论如何都会执行complete
                //超时,status还有success,error等值的情况
                if (textStatus == 'timeout') {
                    $.messager.alert({
                        title: '提示:',
                        msg: '超时',
                        icon: "",
                        width: 350,
                        height: 170,
                        modal: true,
                        closable: false,
                        fn: function (r) {
                            if (r) {
                                // window.location.reload();
                            }
                        }
                    });
                }
            },
            error: function (a, b, c) {
                reject("失败")//会返到alertWindows("提示", err, 2);
            }
        });
    })
}
let obj = getData().then((data) => {
    //得到数据去绑值
    for (var i in data) {
        app.list.push(data[i]);
    }
    //可以继续请求其他接口,或者拿本次获取的值当做传参
    return getData()
}, (err) => {
    alertWindows("提示", err, 2);
}).then((data) => {
    //得到数据
    console.log(data)
}, (err) => {
    alertWindows("提示", err, 2);
})

 

posted @ 2023-03-23 14:18  石头记1  阅读(57)  评论(0)    收藏  举报