skip to Main Content

Hello I am having problems with my setInterval it is only executing only once the if else checks below is my function

const redis = require('redis');
const cache = redis.createClient();

require('./execSocket');

function teste(){

    cache.dbsize(function(err,res){

        if(res){
            console.log(res);

           if(res > 10){

                require('./execSql');
                
            }else{
                
                require('./execSocket');
            }

        }else{
             
             console.log(err);
             require('./execSocket');
        }
    });

}

setInterval(function(){

    teste();

},20000);

when I run the code, in the first run of setInterval it does the normal checks, but in the second run of setInterval it only gives me the number of records saved in bd and does not check if else

2

Answers


  1. Chosen as BEST ANSWER

    I found this way to solve my problem, the code looks like this:

    const redis = require('redis');
    const cache = redis.createClient();
    const shellExec = require('shell-exec');
    
    
    require('./execSocket');
    
    function test(){
    
        cache.dbsize(function(err,res){
    
            if(res){
                console.log(res);
                let i = 10;
               if(res > i){
               
                    console.log("<<<<<wait....>>>>>")
                    console.log("<<<<<saving records....>>>>>")
                    shellExec('node execSql').then(sucess =>{
                        console.log(sucess)
                    }).catch(err =>{
                        console.log(err);
                    });
                    
                }else{
                    console.log("<<<<<The resulting is not yet greater than "+i+" >>>>>");
                    console.log("<<<<<loading....>>>>>");
                    
                    shellExec('node execSocket').then(sucess =>{
                        console.log(sucess)
                    }).catch(err =>{
                        console.log(err);
                    });
                    
                }
    
            }else{
                 
                 console.log(err);
                 require('./execSocket');
            }
        });
    
    }
    
    setInterval(function(){
    
        test();
    
    },20000);

    it's basically solving my problem !


  2. require() loads a module once and then retrieves it from cache and does not executes any code that is located inside the module without executing any function explicitly.

    Suggest exporting a dedicated methods from every method and execute them explicitly in your branches.

    See require for more details.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search