0

First off I thought I'd get this problem solved after this great thread: nodeJs callbacks simple example

However, I am still unsure of how to proceed. Like the title hints at: I need a callback given to a callback who already has node arguments being passed to it

Code:

    (function() 
         var reqs = { 
         http: require('http'),
         path: require('path'),
         fs: require('fs')
    };

    reqs.http.createServer(function (request, response) {

    response.writeHead(200, {
        'Content-Type': 'text/plain'
    });

    response.end('Hello HTTP!');
    }).listen(8080);

    var printCount = function(count) {
        console.log(count);
    };

    var callCount = function(err, list, callback) {
            var count = 0;
            if(err) throw err;
            // console.log(err);
            for (var i = 0; i < list.length; i++) {
                // console.log(reqs.path.extname(list[i]));
                if(reqs.path.extname(list[i]) === ".png" || reqs.path.extname(list[i]) === ".jpg")
                {
                    count++;
                    console.log(count);
                }
            }
            callback(count);
        };

    //count images from executing directory
    var countImages = function(dirName) { 
        var imageCount = reqs.fs.readdir(dirName, callCount(null, null, printCount));
    };

    countImages(__dirname);

})();

I think the key line here is var imageCount = reqs.fs.readdir(dirName, callCount(null, null, printCount)); I'm passing the printCount function to the same function that is called back after fs.readdir asynchronously executes but it seems that me passing null to its first two arguments is overriding Node functionality that passes the callback err and list automatically. How can I get around this? I simply want to count the images in the executing directory and be able to store that value in my main function.

Pretty new to event style programming. Any extra reading suggestions are welcome. There is tons of content out there but I really want to get this up and running for a meeting this weekend. Thanks guys!

Community
  • 1
  • 1
lonious
  • 676
  • 9
  • 25

1 Answers1

1

you can't quite do what you are doing, you are doing callCount(null, null, printCount) which executes the function. But you need to pass a function as a callback. What you want is something like the following, which captures the call back you want and returns a function you can pass as a callback to your api call

 var callCount = function(callback) {
            return function(err, list) {
            var count = 0;
            if(err) throw err;
            // console.log(err);
            for (var i = 0; i < list.length; i++) {
                // console.log(reqs.path.extname(list[i]));
                if(reqs.path.extname(list[i]) === ".png" || reqs.path.extname(list[i]) === ".jpg")
                {
                    count++;
                    console.log(count);
                }
            }
            callback(count);
        }
        }

and then

reqs.fs.readdir(dirName, callCount(printCount));
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • I've changed what callCount is conceptually, its job is to return you a callback that you can use with readdir. Its not a callback in itself. it returns a function that takes errr, list, which is what readdir wants. But it's also "captured" the variabled named "callback" which is set to printcount, so when readdir eventually calls back.... it should use printcount when calling callback – Keith Nicholas Nov 13 '15 at 02:05
  • since readdir is async, it will return straight away.... and at somepoint later it will execute the call back. Only in the call back will you know the count – Keith Nicholas Nov 13 '15 at 02:09
  • This works great! Could I say that `readdir` is asynchronously executed first, then callCount is called(simaltaneously?) which makes `readdir`'s 2nd argument resolve and take on the returned callback which in turn calls my printCount callback? I think I get it conceptually. Hard to put into words. – lonious Nov 13 '15 at 02:13