Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this myFunction().then() operation seem to hang indefinitely?

Tags:

node.js

I am having some trouble with some NON PRODUCTION code. I want to process about 3000 array elements. If I strace the node process, it is sitting at epoll_wait(5, so presumably I am blocking the main thread.

Can anyone suggest either a) what I am doing wrong or b) how I can look at the execution stack / event loop to examine exactly why the code is hanging? I have attempted to debug and step through the code and have that process working but am none the wiser.

UPDATED code using Promises.map:

connection.query(firstPostQuery,{ x: whiteListString }, function( err, rows ) {

    Promise.map(rows, function(result) {


        return sfs.isSpammer({
            ip: result.ip,
            email: result.email,
            username: result.poster
        }).then(function(res) {
console.log(parseInt(res.username.appears) == 1); //evaluates to true

            if (parseInt(res.username.appears) == 1 ) {

                console.log(res.toJSON());
                fs.appendFile(__dirname + '/stopforumspam.txt', res.poster + '\n',
                    function(err) {
                        if (err) {
                            throw err;
                        }
                        return true;
                    });

            } else {
                fs.appendFile(__dirname + '/stopforumspam.txt',
                    'nope\n',
                    function(err) {
                        if (err) {
                            throw err;
                        }
                        return true;
                    });
            }
        });
        //Iteration completed
    }, {concurrency: 5}).then(function(result) {
        //Do something with result
        console.log(result);
    }).catch(function(err) {
        //Error
    });
});

I'm running against node.js 4.2.4. I've been experimenting with Bluebird promises but am unsure if that would be useful in this case as I don't fully understand promises (yet).

like image 715
codecowboy Avatar asked Jan 19 '16 10:01

codecowboy


1 Answers

You could try to use (bluebird) Promise.map to iterate async.

connection.query(firstPostQuery, {x: whiteListString}, function(err, rows) {

    Promise.map(rows, function(result, index) {
        console.log('item', index);
        return new Promise(function(resolve, reject) {
            sfs.isSpammer({
                ip: result.ip,
                email: result.email,
                username: result.poster
            }).then(function(res) {
                console.log('In Promise', res);
                console.log(parseInt(res.username.appears) == 1); //evaluates to
                                                                  // true
                if (res && parseInt(res.username.appears) == 1) {
                    return fs.appendFile(__dirname + '/stopforumspam.txt', res.poster + '\n',
                            function(err) {
                                console.log('In AppendFile spamer');
                                if (err) {
                                    reject(err);
                                }
                                resolve(true);
                            });
                } else {
                    return fs.appendFile(__dirname + '/stopforumspam.txt',
                            'nope\n',
                            function(err) {
                                console.log('In AppendFile good user');
                                if (err) {
                                    reject(err);
                                }
                                resolve(true);
                            });
                }
            });
        });
    }).then(function(res) {
        console.log(res);
    }).catch(function(err) {
        console.log(err);
    });
});
like image 166
Nazar Sakharenko Avatar answered Oct 26 '22 00:10

Nazar Sakharenko