当前位置: 首页 > 工具软件 > CODE2RACE > 使用案例 >

利用promise.race来实现请求超时埋点功能

陶涵育
2023-12-01
// 查询
        queryTickets (index) {
            this.tsRefresh[index] = false;
            this.$forceUpdate();
            let reason, code; //埋点用
            Promise.race([new Promise((resolve, reject) => {
                this.$model.xxx[this.queryMethodArr[index]](this.queryParamArr[index], res => {
                    code = res.result.code;
                    if (res && code == "0000" && res.data && JSON.stringify(res.data) != "{}") {
                        this.queryTicketsCallBack({
                            index,
                            data: res.data
                        });
                        if (index == 0) {
                            this.queryFinishedCallback();
                        }
                        resolve("请求成功");
                    } else {
                        if (JSON.stringify(res.data) == "{}") {
                            reason = "结构错误";
                        } else {
                            reason = "没有返回结果";
                        }
                        reject("没有返回结果");
                    }
                });
            }), new Promise((resolve, reject) => {
                setTimeout(() => {
                    reason = "请求超时";
                    reject("请求超时");
                }, 6000);
            })]).catch((e) => {
                // 埋点
                let tval = "";
                try {
                    let mdval = {
                        index,
                    };
                    console.log(mdval, "mdval");
                    tval = encodeURIComponent(JSON.stringify(mdval));
                } catch (error) {
                    tval = "";
                }
                this.$tcTrackEvent("queryerror", "action", e, tval);

                this.$tcTrackEvent("", "show", "", JSON.stringify({
                    reason: "错误",
                    content: "错误~"
                }));

                if (!this.dealTimeOut) {
                    this.dealTimeOut = true;
                    // postmessage给小程序刷新
                    window.wx && wx.miniProgram && wx.miniProgram.postMessage({ data: "xxx" });

                    this.$alert({
                        title: "温馨提示",
                        message: "错误",
                        callback: () => {
                            if (this.$env.isMiniProgram&&this.query.planCode!="xxx") { 
                                wx.miniProgram.navigateBack();
                            } else {
                                this.$router.back();
                            }
                        },
                        buttons: [{
                            text: "返回列表"
                        }]
                    });
                }
            });
        },

 

 类似资料: