Home > Software engineering >  Send multiple requests separate instead of array in Javascript
Send multiple requests separate instead of array in Javascript

Time:05-01

This is part of code (code not mine, I can't understand how it's working, maybe it's called Promise, but I'm not sure).

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")
            }))
        }
    }
},

This code collecting data of users, store in to array and then pass it to the server. So it sending big array of all users in one request (it pushing all entries to array in this part n.push('{"__class__":"ServerRequest"...).

What I want to achieve is to send requests for each user one by one with some delays (for example 1 second delay between requests).

I've tried many ways to achieve it, but all unsuccessfully, I'm lack of knowledge about this programming language.

I've tried to use setTimeout functions in different ways, but all the time unsuccessfully:

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")
        }))
    }
}

Also tried like this:

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")
        }))
    }
}

UPDATED

As per comment asked what is %%requestId%% - I found this part:

{
    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()
        }))
    }))
}

CodePudding user response:

try this

in general, forEach should be replaced with for..of loop with using async/await helper function which will delay the request (in await delaySome(1000); part)

it should send data in the same format, and if it fails at the server, that's probably where it needs tweaking.. or everywhere else..

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")
                }
            });
        },
    },
};
  • Related