Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

repeat string n times javascript

"a".repeat(10)
Comment

repeat 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 :: vuejs scroll to top 
Javascript :: javascript string unique characters 
Javascript :: datatable hide columns 
Javascript :: react native socket io 
Javascript :: react native zindex issue on android 
Javascript :: express cors error 
Javascript :: string replace javascript 
Javascript :: encrypt data using SHA256 algorithm in JavaScript 
Javascript :: link script react17 
Javascript :: laravel query json 
Javascript :: async storage get item 
Javascript :: node get root directory 
Javascript :: nested shorthand if javascript 
Javascript :: javascript transitionduration 
Javascript :: convert object to array javascript 
Javascript :: javascript go to div id 
Javascript :: livewire upload file progress indicator 
Javascript :: react native submit on enter key 
Javascript :: js sort array of objects 
Javascript :: javascript string change character at index 
Javascript :: how do i make a link to direct me to a new page when i click on a button in react 
Javascript :: javascript string remove backslash 
Javascript :: for loop condition javascript 
Javascript :: remove one array from another javascript 
Javascript :: change checkbox jquery alert 
Javascript :: object exists in array javascript 
Javascript :: two decimel javascript 
Javascript :: javascript merge two objects 
Javascript :: how to get ip address javascript 
Javascript :: React does not recognize the `activeClassName` prop on a DOM element 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =