Answer the question
In order to leave comments, you need to log in
How to improve query packaging?
I'm trying to implement a feature to package requests. The essence of its work is to collect requests in a pack of 25 pieces and send them with an interval of 50ms. Upon execution, the response should return to the function that sent the request.
I'm new to javascript, it would be helpful to hear criticism from advanced developers.
// Пример вызова функции
await vk("get.user", { user_id: 1 });
let queue = [];
function vk(method, data = {}) {
return new Promise((resolve, reject) => {
const query = `API.${method}(${JSON.stringify(data)})`;
queue.push({ resolve, reject, query });
});
};
setTimeout(async function execute() {
const queuePart = queue.splice(0, 25);
if (queuePart.length !== 0) {
const code = `return [${queuePart.reduce((chunk, { query }) => `${chunk}${query},`, "")}];`;
const response = await request(code);
for (let i = 0, len = response.length; i < len; i++) {
const { resolve } = queuePart[i];
resolve(response[i][0]);
};
};
setTimeout(execute, 50);
}, 50);
Answer the question
In order to leave comments, you need to log in
resolve(response[i][0]);
const { response, execute_errors } = await request(code);
for (let i = 0, len = response.length; i < len; i++) {
const { resolve, reject } = queuePart[i];
if (response[i] === false) {
reject(execute_errors.shift());
} else {
resolve(response[i]);
}
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question