abigwonderful
abigwonderful

Reputation: 1927

javascript variable assignment from chain of functions

Trying to dumb this one down a bit:

    //first named function
    function tryMe(x){
        tryMeAgain(x);
    };

    //second named function
    function tryMeAgain(y){
        return y;
    };

    //assign the result of first function (which will actually be the result of the second function) to a var
    var testTry = tryMe('worth a shot');

    console.log(testTry); //undefined! But I would like 'testTry' to return 'worth a shot'

So i've got two questions:

  1. Why is this?
  2. How to I assign 'testTry' properly?

EDIT BELOW: So the responses all make sense and I think my problem may lay somewhere else in my code. My attempt to simplify the question may have overlooked another piece of the puzzle. I'm including a new version and hoping you all can shed some light:

  var runtime = (function(){

        var $jq = jQuery.noConflict(true);

        function Runtime(){
            this.$jq = $jq;
        }

        Runtime.prototype.method1 = function( _value, _callback ){
            setTimeout(function(){ console.log('dependency1_resolved');
                _callback.apply(this, [{valIs:_value}]);
            }.bind(this), (Math.random() * 1000));
        };

        Runtime.prototype.method2 = function( _value, _callback ){
            var self = this;
            setTimeout(function(){ console.log('dependency2_resolved');
                _callback.apply(self, [{differntValIs:3}]);
            }.bind(this), (Math.random() * 1000));
        };

        Runtime.prototype.method3 = function( _value, _callback ){
            setTimeout(function(){ console.log('dependency3_resolved');
                _callback.apply(this, [{valIs:_value['differntValIs'] *= 4}]);
            }.bind(this), (Math.random() * 1000));
        };

        return new Runtime();
    })();


  runtime.initialize(function( $ ){
    function firstCalc(firstInput){
        return runtime.method1(firstInput,secondCalc);
    };
    function secondCalc(secondInput){
        return runtime.method2(secondInput, thirdCalc);
    };
    function thirdCalc(thirdInput){
        return runtime.method3(thirdInput, fourthCalc);
    };
    function fourthCalc(ourResult){
        //console.log( ourResult );
        return ourResult;
    };

    var _value = firstCalc(4); //this is undefined!!
});

Upvotes: 0

Views: 84

Answers (4)

domehead100
domehead100

Reputation: 161

Let's follow the function calls starting with:

var _value = firstCalc(4);

What is the result of firstCalc(4)? Well, firstCalc() returns the result of runtime.method1().

What's the result of runtime.method1(4, secondCalc)? It's undefined, because runtime.method1() simply does a setTimeout to call secondCalc() later, then it returns. There is no explicit return statement, so the default behavior is to return undefined.

That's where your initial expression "var _value = firstCalc(4)" ends.

The setTimeouts cause the other functions to be called after random delays, and indeed inside of fourthCalc() the variable ourResult should be { valIs: 12 }, but this occurs later, after the setTimeouts have caused the various calc functions to be called. Also, the "return ourResult" in fourthCalc returns ourResult to... whom? No one; there is nothing assigning the result of fourthCalc to anything.

Try this:

var _value = firstCalc(4); //this is undefined!!
setTimeout(function() { console.log(_value); }, 5000);

Upvotes: 0

Sindhoo Oad
Sindhoo Oad

Reputation: 1194

your whole code is right just with minor mistake.

  function tryMe(x){
             tryMeAgain(x);
         };

in this you are just calling the function but not returning that value who is calling it, then definitely

 var testTry = tryMe('worth a shot');

will be undefined

change it to

function tryMe(x){
     return  tryMeAgain(x);
 };

EDIT

 function firstCalc(firstInput){
        return runtime.method1(firstInput,secondCalc);
    };

your firstCalc is calling secondCalc and secondCalc calling thirdCalc, but what you are missing is your firstCalc , secondCalc and thirdCalc are functions and your passing these values as variable which does not have any value call them as function.

function firstCalc(firstInput){
        var secondCalc = secondCalc(thiValueYouWantToCalculate);
            return runtime.method1(firstInput,secondCalc);
        };

Upvotes: 0

leo.fcx
leo.fcx

Reputation: 6457

Default return value of a function is undefined unless you state otherwise. And that is what is happening in your code, testTry returns undefined

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

You are returning the value from tryMeAgain but not from tryMe method, so the default value returned is undefined

//first named function
function tryMe(x) {
  return tryMeAgain(x);//need to return the value returned by tryMeAgain to the caller of tryMe
};

//second named function
function tryMeAgain(y) {
  return y;
};

//assign the result of first function to a var
var testTry = tryMe('worth a shot');

snippet.log(testTry);
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

Upvotes: 1

Related Questions