Pritam Karmakar
Pritam Karmakar

Reputation: 2801

Nested function call nodejs

Sorry for this naive question, just started learning Nodejs. Could you please let me know why the control never coming to this line - console.log("inside ScrapePage callback") ? Thanks for looking into this and appreciate your help.

var CronJob = require('cron').CronJob;

new CronJob('* * * * * *', function() {
    var collection = db.collection('webpages');
    collection.find({}, function(e, docs) {
// iterate over the webpage list
        for (var i = 0; i < docs.length; i++) {
            var webpage = docs[i];
            (function (webpage) {
                DoStatusCheck(webpage);
            })(webpage);
        }
    });
}, null, true, "America/Los_Angeles");

function DoStatusCheck(webpage) {
    ScrapePage(webpage, function(error, value){
        console.log("inside ScrapePage callback");
    })
}
function ScrapePage(webpage)
{
    return "inside ScrapePage function";
}

Upvotes: 0

Views: 1334

Answers (2)

Matt Harrison
Matt Harrison

Reputation: 13567

You never call the callback. You should have ScrapePage accept a second parameter:

function ScrapePage(webpage, callback) {}

And then call that callback once it has finished doing whatever work it needs to, passing in the error and result:

function ScrapePage(webpage, callback) {

    doSomethingAsync(function (err, result) {

        if (err) {
            return callback(err, null);
        }

        callback(null, result);
    });
}

Upvotes: 0

Ben Taber
Ben Taber

Reputation: 6741

ScrapePage is not calling the callback that you are passing to it.

Edit it to call that second argument.

function ScrapePage(webpage, cb)
{
  cb(null, "value");
}

Upvotes: 1

Related Questions