Search code examples
javascriptfunctionvariablesreturn

Javascript return function in another function


I have a javascript which is like this

function one (var1, var2){
    var var3= var1+var2;
    return var3;
}

function two( var4, var5){
    var var6=var4*var5;
    return var6;
}

function three(var7, var8){
    var9=var7/var8;
    return var9;
}

var first = one(2, 4);

var second= two(first, 8);

var third= three(second, 9);

I want all the function to be separate because they each are like module which would handle different operations. So I do not want to use callback, I have tried

$.ajax(first = one(2,4))
.then({second=two(first, 8)})
.then(three(second,9))

I have also tried this

$.ajax()
.then(function(first=one(2,4){
    return first;
})
.then(function(second=two(first,4){
    return second;
})

I have also tried this

$.ajax({
    first:one(2,4),
    second:two(first,4),
    third:three(second,9),
})
.then(function(first=one(2,4){
    return first;
})
.then(function(second=two(first,4){
    return second;
})

this may sound funny but I even tried

var third= three(two(one(2, 4), 8), 9);

amongst many others. All of them show operation takes place with the first function and other functions but the it does not give me result from previous function


This is an update to the above. Modification

I do not want to use promise in the other functions as some other functions would also call those functions and they would not be expecting a promise but a valid data

function modifyData(details){
    for(var x=0 ;x<array.length;x++){
        //this would do a massive loop which would
        //return a json string that would be 
    }
 }


 function tableFunction(url, tableNameVar){
    //json fields
 var details=[];
 details[0] = 'name'; 
 details[1] = 'sex'; 
 details[2] = 'eye_color'; 
 details[3] = 'height'; 
 details[4] = 'body_built'; 
        var jsonData='';
        var main = "";
    $(document).ready(function(){ 
        $('#'+tableNameVar+'').dataTable({
            destroy: true,
            data: JSON.parse(extraFunctionProcess(modifyData(details),details)),
            columns:[
                     {title: "Name", data:"name"} ,
                     {title: "Gender", data:"sex"} ,
                     {title: "Eye color", data:"eye_color"} ,
                     {title: "Height", data:"height"} ,
                     {title: "Body built", data:"body_built"} 
            ]
        });
    });
}

I want to process the data and put it inside the table. The extraFunctionProcess has worked if the data is passed straight into the data field without putting it inside the modifyData function. which is instead of the above it is

data: JSON.parse(extraFunctionProcess(fullData,details))

This has worked but due to the modification which I have done, it has brought up an error in my browser console which is undefined inside the extraFunctionProcess.

I just shortened the code into I put online now.


Solution

  • I have actually found a solution to the problem. The modifyData function was the one with the problem. The return was inside another function. Intead of the following

    function modifyData(details){
        for(var x=0 ;x<array.length;x++){
            //this would do a massive loop which would
            //return a json string that would be inside a function
        }
     }
    
    
    function modifyData(details){
        for(var x=0 ;x<array.length;x++){
             return functionName(callback to be done);
            //this would do a massive loop which would
            //return a json string that would be 
        }
     }