Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

repeat network call n times in 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
  })
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to do addition in javascript 
Javascript :: javascript push array 
Javascript :: js sleep function 
Javascript :: json with postgresql 
Javascript :: fake delay in fetch 
Javascript :: node js mysql variables 
Javascript :: postfix date javascript 
Javascript :: discord.js find word inside comment 
Javascript :: write an array that invokes the alter function in to the array 
Javascript :: test cases in react 
Javascript :: jquery-3.5.0.min.js 
Javascript :: javascript variable as object key 
Javascript :: Firebase: Error (auth/invalid-api-key). 
Javascript :: how to build with a specific .env file node 
Javascript :: shopify get list of all products ajax api 
Javascript :: kendo js add one day to a date 
Javascript :: vscode new file shortcut 
Javascript :: how to get max value from array of objects in javascript 
Javascript :: split the string on any and all periods, question mark using regex 
Javascript :: split by space capital letter or underscore javascript 
Javascript :: merge in mongodb 
Javascript :: js how to sort array by object value 
Javascript :: quasar router authentication 
Javascript :: javascript latitude longitude to km 
Javascript :: Highlight current nav link in react 
Javascript :: get last element of array javascript 
Javascript :: javascript getter 
Javascript :: datepicker auto select off 
Javascript :: javascript compress base64 image 
Javascript :: random between min and max 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =