在 Javascript 中单独发送多个请求而不是数组

Send multiple requests separate instead of array in Javascript

这是代码的一部分(代码不是我的,我不明白它是如何工作的,也许它叫做 Promise,但我不确定)。

m = {
    mounted: function() {
        var e = this;
        this.$bus.on("buff-event", (function(t) {
            e.buff(t)
        }))
    },
    methods: {
        buff: function(e) {
            var t = this;
            this.$bus.emit("bfceebecbb-change", "Motivating...");
            var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
            this.requestFaker.fetch(n).then((function(i) {
                i = (i = i.filter((function(t) {
                    return t.requestMethod == e
                }))[0]).responseData.filter((function(e) {
                    return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
                })), n = [], _.forEach(i, (function(e) {
                    n.push('{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
                })), n.length ? (n = "[" + n.join(",") + "]", t.requestFaker.fetch(n).then((function() {
                    t.$bus.emit("bfceebecbb-change", "Motivation success")
                }))) : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
            }))
        }
    }
},

这段代码收集用户的数据,存储到数组中,然后传递给服务器。因此它在一个请求中发送所有用户的大数组(它在这部分将所有条目推送到数组 n.push('{"__class__":"ServerRequest"...)。

我想要实现的是为每个用户一个一个地发送请求,并有一些延迟(例如请求之间延迟 1 秒)。

我尝试了很多方法来实现它,但都没有成功,我对这种编程语言缺乏了解。

我尝试过以不同的方式使用 setTimeout 函数,但一直没有成功:

methods: {
    buff: function(e) {
        var t = this;
        this.$bus.emit("bfceebecbb-change", "Motivating...");
        var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
        this.requestFaker.fetch(n).then((function(i) {
            i = (i = i.filter((function(t) {
                return t.requestMethod == e
            }))[0]).responseData.filter((function(e) {
                return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
            })), n = [], _.forEach(i, (function(e) {
                n.push('{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
            })), n.length ? 
            setTimeout((function() {
            (setTimeout((function() {n = "[" + n.join(",") + "]"}), 1000) , t.requestFaker.fetch(n).then((function() {
                t.$bus.emit("bfceebecbb-change", "Motivation success")
            })))}), 1000)  : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
        }))
    }
}

也试过这样:

methods: {
    buff: function(e) {
        var t = this;
        this.$bus.emit("bfceebecbb-change", "Motivating...");
        var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
        this.requestFaker.fetch(n).then((function(i) {
            i = (i = i.filter((function(t) {
                return t.requestMethod == e
            }))[0]).responseData.filter((function(e) {
                return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted)
            })), n = [], _.forEach(i, (function(e) {
                n.push('{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}')
            })), n.length ? 
            setTimeout((function() {
            (n = "[" + n.join(",") + "]", t.requestFaker.fetch(n).then((function() {
                t.$bus.emit("bfceebecbb-change", "Motivation success")
            })))}), 1000)  : t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
        }))
    }
}

已更新

根据评论询问什么是 %%requestId%% - 我找到了这部分:

{
    key: "fetch",
    value: function(e) {
        function t(t) {
            return e.apply(this, arguments)
        }
        return t.toString = function() {
            return e.toString()
        }, t
    }((function(e) {
        for (var t = e;
            (t = e.replace("%%requestId%%", this.requestId)) !== e;) e = t, this.incRequestId();
        return fetch(gameVars.gatewayUrl, this.getHeadForFetchQuery(e)).then((function(e) {
            return e.json()
        }))
    }))
}

试试这个

一般来说,forEach 应替换为 for..of 循环,并使用 async/await 辅助函数来延迟请求(在 await delaySome(1000); 部分)

它应该以相同的格式发送数据,如果它在服务器上失败,那可能是它需要调整的地方..或其他地方..

m = {
    mounted: function() {
        var e = this;
        this.$bus.on("buff-event", function(t) {
            e.buff(t);
        });
    },
    methods: {
        buff: function(e) {
            var t = this;


            function delaySome(delay) {
                return new Promise((resolve) => {
                    setTimeout(() => {
                        resolve();
                    }, delay);
                });
            }

            this.$bus.emit("bfceebecbb-change", "Motivating...");
            var n = '[{"__class__":"ServerRequest","requestData":[],"requestClass":"OtherPlayerService","requestMethod":"' + e + '","requestId":%%requestId%%}]';
            this.requestFaker.fetch(n).then(async function(i) {
                (i = (i = i.filter(function(t) {
                    return t.requestMethod == e;
                })[0]).responseData.filter(function(e) {
                    return void 0 === e.next_interaction_in && !e.is_self && (void 0 === e.accepted || 1 == e.accepted);
                })),
                (n = []);


                if (i.length) {
                    for (const e of i) {

                        await delaySome(1000);

                        t.requestFaker.fetch('[{"__class__":"ServerRequest","requestData":[' + e.player_id + '],"requestClass":"OtherPlayerService","requestMethod":"polivateRandomBuilding","requestId":%%requestId%%}]').then(function() {
                            t.$bus.emit("bfceebecbb-change", "Motivation success");
                        })

                    }
                } else {
                    t.$bus.emit("bfceebecbb-change", "Nobody to motivate")
                }
            });
        },
    },
};