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

109
Views
NodeJS - Function executing three times

When the function is invoked it runs three times with both if and else statements being executed. However, the other functions are working fine where there are no 'if else' statements. So, How can I solve this? This is the code:

**From todo.js**

    const tasks = require("./tasks");

    yargs.command({
      command: "Add",
      builder: {
        task: {
          demandOption: true,
        },
        deadline: {
          demandOption: true,
        },
      },
      handler(argv) {
        tasks.addTask(argv.task, argv.deadline);
      },
    }).argv;

**From tasks.js**

    //Add Tasks!!

    const addTask = (task, deadline) => {
      const tasks = loadTasks();
      const duplicateTask = tasks.find((taskk) => taskk.task === task);
      if (!duplicateTask) {
        tasks.push({
          task: task,
          deadline: deadline,
        });
        saveTasks(tasks);
        console.log(chalk.green.inverse("New task added successfully!"));
      } else {
        console.log("Task already exist!");
      }
    };

    // Load Tasks!!
    const loadTasks = () => {
      try {
        const bufferData = fs.readFileSync("tasks.json").toString();
        return JSON.parse(bufferData);
      } catch (e) {
        return [];
      }
    };

    //Save Tasks!!
    const saveTasks = (task) => {
      const saveTask = JSON.stringify(task);
      fs.writeFileSync("tasks.json", saveTask);
    };

Output

about 4 years ago · Juan Pablo Isaza
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!