Повторите сетевой вызов N Times в JS
// repeat call n times with identical parameters
// call is an async function
export const repeatCall = (call, {
repeat = 1,
params = [] // params is an array containing the parameters to `call` in the expected sequence
}) => {
const calls = [];
for (let i = 0; i < repeat; i++) {
calls.push(call(...params));
}
return calls;
}
// repeat call n times with unique parameters for each call
export const repeatUniqueCall = (call, {
params = [] // params should be an array of arrays where each nested array with index i represents the params for call i
}) => {
const calls = [];
for (let i = 0; i < params.length; i++) {
calls.push(call(...params[i]));
}
return calls;
}
// usage below
const myAsyncFunction = async (param1, param2) => {
return await doSomething(param1, param2);
};
Promise.all(
repeatCall(
myAsyncFunction,
{
repeat: 3,
params: ['value1', 'value2'],
}
))
.then((responses) => {
// do something with responses
})
Anxious Alligator