2

Pulling my hair out here trying to understand the infuriating nuances of Javascript. Hopefully some JS guru can take a gander, point and go, "well, there's yer problem...".

Here's a slimmed down sample of the problem:

var Parent = (function () {
    var func1 = function () {
        func2(function (res) {
            console.log(res);
        });
    };

    var func2 = function (callback) {
        callback('abc');
    };

    return {
        init: function () {
            func1();
            func2();
        }
    };
})();

Call with Parent.init();

This fails with the error:

Uncaught TypeError: callback is not a function
    at check2 (<anonymous>:9:9)
    at Object.init (<anonymous>:15:13)
    at <anonymous>:1:8

What's getting me, is that if I comment out the enclosing code, like so, then it works as expected:

// var Parent = (function () {
    var func1 = function () {
        func2(function (res) {
            console.log(res);
        });
    };

    var func2 = function (callback) {
        callback('abc');
    };

    // return {
    //     init: function () {
    //         func1();
    //         func2();
    //     }
    // };
// })();

...and call with func1();

Result:

abc

What am I missing?

Thanks

5
  • 5
    Your function is written with a parameter, but when you call it you pass nothing. Commented Sep 23, 2020 at 13:19
  • 1
    To turn the question around... When you call func2(); what do you expect callback to be and why? Commented Sep 23, 2020 at 13:20
  • 1
    In your init() function, you're calling func2();, without specifying the callback function as the required argument. In your second version (with init commented out), you're correctly specifying the callback function in func2(function (res) { ... });. Commented Sep 23, 2020 at 13:20
  • func2() must do some processing and return a result. My (perhaps screwed) understanding is that callback must call the "calling" anon function in func1(). lol, I dunno, hence the question. I just want to know why it works in the one sample, but not the other. Commented Sep 23, 2020 at 13:23
  • @Reece - mmm, ok, let me give that a try. Sounds like sound advice. Commented Sep 23, 2020 at 13:24

3 Answers 3

1

In your version, you're calling func2() without specifying the callback function, which is a required argument. In your second example (with init commented out), you're correctly specifying the callback function in func2(function (res) { ... });.

Is the below snippet something you're looking for?

const Parent = (function () {
  const func1 = function () {
    func2(function (res) {
      console.log(res);
    });
  }

  const func2 = function (callback) {
    callback('abc'); // this is passing 'abc' to line 3 of this snippet
  }

  return {
    init: function () {
      func1();
      // func2(); // you don't want to call "func2" here, as "func1" calls it
      // Or you could run:
      func2(function (res) {
        console.log(res);
      });
      // But this makes "func1" redundant
    }
  };
});

Parent().init();

// Output
abc
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Reece, that works and makes sense. Much appreciated!
If this answer helped you solve your question, please mark it as the solution :-). Happy to help!
...just waiting for the mandatory countdown to end so I can click that button!
Cheers Henry! Glad I was able to help.
1

You have to pass callback parameter into func2 inside init. something like this.

init: function () {
            func1();
            func2(function (res) {
                 console.log(res);
            });
      }

Comments

1

From a comment on the question:

I just want to know why it works in the one sample, but not the other.

Because in one example you pass an argument to func2 and in the other you don't. Look at the working version:

func2(function (res) {
    console.log(res);
});

vs. the non-working version:

func2();

The difference is that the first one passes a function which gets invoked as callback('abc'); whereas the second one passes nothing, so the attempt to invoke the non-existant callback function fails.


As an aside, in your non-working example you call func2 twice, once with the callback and once without. So it both "works" and "fails" in that case.

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.