Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
How to excute a function only once for same argument

My function call is given below:

await insertingMatchIdsInAllTeamPlayers(fieldersA, matchID)

Suppose the function is called with matchID '1', it should get executed, but if the function is called again with matchId '1'(It will in my case), it should not be executed. However, if it is called with id '2' (basically id !== '1'), it should be executed. I don't care for fieldersA argument.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could track all passed arguments in array outside of the function. When you call the function, it will check, if the supplied argument is in the array. If it's not, call the function and insert the argument into the array. If the argument is already in the array, don't call the function.

const suppliedMatchIDs = [];

function insertingMatchIdsInAllTeamPlayers(fieldersA, matchID) {
    if (suppliedMatchIDs.includes(matchID)) {
      return;
    } else {
      suppliedMatchIDs.push(matchID);
    }

    // Your function here
}

The general concept of caching arguments to speed up function calls is called memoization.

about 4 years ago · Juan Pablo Isaza Report

0

const matchIdFirstTimeOne = true
if(matchId === 1 && matchIdFirstTimeOne) {
  await insertingMatchIdsInAllTeamPlayers(fieldersA, matchID);
  matchIFirstTimeOne = false
}
about 4 years ago · Juan Pablo Isaza Report

0

Using closure could solve it.

var insertingMatchIdsInAllTeamPlayers = (function() {
    var executed = [];
    return function(fieldersA,val) {
        if (executed.indexOf(val) == -1) {
            executed.push(val);
            console.log(val);
        }
    };
})();

insertingMatchIdsInAllTeamPlayers('',1); // console.log(1)
insertingMatchIdsInAllTeamPlayers('',1); // 
insertingMatchIdsInAllTeamPlayers('',2); // console.log(2)
insertingMatchIdsInAllTeamPlayers('',2); // 
insertingMatchIdsInAllTeamPlayers('',3); // console.log(3)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!