2

I was always wondering how are callback implemented. I always wandered (in Node.js) how does this work:

fs.readFile('mydata.txt', function (err, buffer) {

What returns an err? What returns a buffer?

I know these are callbacks, and I always strugled to learn them. So, I wrote my own simple implementation in order to understand it.

Can someone please see this simple example. It comes from my head, since I took my time to understand it what I do. Of course, this might be totally wrong.

I would love to hear your comments. Before I abandon Callback in favor of Promises, I want to 100%understand them!!!

My Example:

 function Executor (callback, nameParam) {
              callback(1,6, function (err, result) {
                       if (err) {
                           console.log(err);
                       } else {
                           console.log(result)
                       }
              });
        }

        function calculator(num1, num2, returnStuff) {
            var err, result, internalResult;

            internalResult = num1 + num2;

            if (internalResult < 3) {
              returnStuff(err = "Number is lower then three", result);
            } else {
              return returnStuff (err, result = "Number is larger then three");
            }
        }

         Executor(calculator, "John");
DonaldM
  • 53
  • 5
  • possible duplicate of [Create a custom callback in JavaScript](http://stackoverflow.com/questions/2190850/create-a-custom-callback-in-javascript) – Matthew Bakaitis Jun 19 '14 at 14:19
  • 2
    As far as I can see or understand... there is no answer that answeres my implementation in the Thread suggested above. – DonaldM Jun 19 '14 at 14:30
  • My comment was **not** a criticism of your interest to understand callbacks. I think it's an important topic and many do not know the fundamentals that allow for this. That said, your code works. There's nothing to fix, though some may suggest style changes. The referenced answer touches upon things you need to "100% understand" callbacks, 'next steps' to explore as it were. Also worth noting, this is a topic for entire chapters of books...which is a bit more than you can get from a StackOverflow answer. – Matthew Bakaitis Jun 19 '14 at 14:53
  • 2
    @Matt Bakaitis No criticism taken. I appreciate the link, and will definitely go over it. I was merely eager that someone checks what I have come up with. Like a reward after hours of pondering and reading. – DonaldM Jun 19 '14 at 14:57
  • One suggestion: try changing the `Executor` function argument to `(callback, nameParam)` and the title of the function within `Executor` to `callback` as well...then write another function that accepts the same number of arguments as `calculator` and experiment with passing each into `Executor`...even in the same script. That will help give some perspectives. Also, if you aren't comfortable with the term 'first class function', check out some of the books or tutorials on javascript that explore this term's importance. – Matthew Bakaitis Jun 19 '14 at 15:01

0 Answers0