Thursday, 11 May 2017

Getting out of callback hell

I am still trying to wrap my head around asynchronous programming, how could I make this code neater using callbacks? I am using the Async package in another part of the file.

For next time, what are some best practices when working with routes that contain a lot of functionality? Thank you very much!

app.get('/results', function(req, res) {

    var data = fs.readFileSync('./output.json', null);


    JSON.parse(data, function(key, value) {

        if (key == "message") {
            cb(value);
        }

        function cb(value, err) {
            if (err) throw err;

            tone_analyzer.tone({
                    text: value
                },
                function(err, tone) {

                    if (err)
                        console.log(err);
                    else {

                        var data = tone.document_tone;
                        for (i in data) {
                            // //Gets emotional tones
                            console.log(data[i][0]);

                            //Gets language tones
                            console.log(data[i][1]);

                            //Gets social tones
                            console.log(data[i][2]);
                        }
                    }

                });
        }
    });

    res.render('results', {
        // emotion.anger
    });
})



via agomez

No comments:

Post a Comment