1

I have the following javascript code which I am unable to pass into "inner function".

I'm able to retrieve u1 and p1 values in the "Outer Function" console while I'm unable to retrieve it in the function which is nested inside 'confirm'.

Console prints Inner Function's u1 and p1 as undefined.

The code snippet as shown:

function alertFunc(vara, varb) {
    console.log("Timeout!");
    var u1 = vara;
    var p1 = varb;
    console.log("Outer Function: " + u1 + " , " + p1);

    confirm(u1, p1);

    function confirm(u1, p1) {
        var confirmPopup = $ionicPopup.confirm({
            title: 'Network Time-Out',
            template: 'Check your network connection and try again.',
            okText: 'Retry'
        });
        confirmPopup.then(function(res, u1, p1) {
            if (res) {
                $scope.loading = true;
                console.log("Inner Function: " + u1 + " , " + p1);
                loginz(u1, p1);
            } else {
                console.log('Cancelled');
            }
        });
    }
}

What could be the problem as I'm sure I passed in the variables in. Could it be during the 'confirmPopup', I did not pass the values in? Would really appreciate any pointers!

6
  • Alright. Hold on lemme format it. Thanks! Commented Dec 4, 2015 at 9:20
  • @T.J.Crowder Thanks for the edit! Commented Dec 4, 2015 at 9:21
  • 1
    mate try changing confirmPopup.then(function(res) Commented Dec 4, 2015 at 9:23
  • 1
    Like the answer below, the main issue is probably that the callback for a promise does not include those values as arguments. Commented Dec 4, 2015 at 9:23
  • @Alex yeap it worked thanks mate! Commented Dec 4, 2015 at 9:50

1 Answer 1

4

What could be the problem as I'm sure I passed in the variables in.

That depends entirely on what the confirmPopup function uses as its resolution value, but the odds are that no, you haven't passed them in.

You also don't need to: Just remove them from the then callback's function argument list, and you'll be able to access them as that callback is a closure over the context where it's created, which has u1 and p1 already.

E.g.:

function alertFunc(vara, varb) {
    console.log("Timeout!");
    var u1 = vara;
    var p1 = varb;
    console.log("Outer Function: " + u1 + " , " + p1);

    confirm(u1, p1);

    function confirm(u1, p1) {
        var confirmPopup = $ionicPopup.confirm({
            title: 'Network Time-Out',
            template: 'Check your network connection and try again.',
            okText: 'Retry'
        });
        confirmPopup.then(function(res) {
            // removed from here -----^
            if (res) {
                $scope.loading = true;
                console.log("Inner Function: " + u1 + " , " + p1);
                loginz(u1, p1);
            } else {
                console.log('Cancelled');
            }
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.