-1

I have used wikipedia-js for this project. This is my code for summary.js file.

var wikipedia = require("wikipedia-js");
var something = "initial";
module.exports = {
    wikitext: function(topicname) {
        console.log("Inside wikitex funciton :" + topicname);
        var options = {
            query: topicname,
            format: "html",
            summaryOnly: false,
            lang: "en"
        };

        wikipedia.searchArticle(options, function(err, htmlWikiText) {
            console.log("Inside seararticlefunciton :");

            if (err) {
                console.log("An error occurred[query=%s, error=%s]", topicname, err);
                return;
            }
            console.log("Query successful[query=%s, html-formatted-wiki-text=%s]", topicname, htmlWikiText);
            something = htmlWikiText;
        });
        return something;
    },
};

This module I am using in /wiki/:topicname route. The corresponding code in index.js is like this.

router.get('/wiki/:topicname', function(req, res, next) {
    var topicname = req.params.topicname;
    console.log(topicname);
    var first = summary.wikitext(topicname);
    res.send("Hello "+first);
});

The problem is, everytime i visit a wiki/some-topic, the last return statement of summary.js executes before htmlWikiText is populated with content. So I always see hello initial on the browser page. Although after sometime it gets printed on terminal due to console.log statement.

So how should I resolve this issue?

Riken Shah
  • 3,022
  • 5
  • 29
  • 56
  • It is likely impossible to make it synchronous, read the docs for `wikipedia` to find out for sure. Either way, making it synchronous is a HORRIBLE idea anyway in your situation because it will lock down your server for a period of time, making it unable to respond to any requests. – Kevin B Jan 26 '16 at 17:16
  • so how can I return the value only after the above function has finished execution? also nothing of synchronous or asynchronous is mentioned in the docs. – Riken Shah Jan 26 '16 at 17:21
  • you can't, that's impossible. you must use a callback. – Kevin B Jan 26 '16 at 17:25
  • ok. I tried using callback in wikipedia.searchArticle function, but isn't working. How should use callback in that function? – Riken Shah Jan 26 '16 at 17:29
  • 1
    You'll need to understand callbacks in order to successfully work with nodejs. Read an article like github.com/maxogden/art-of-node#callbacks and you'll be able to answer your own question. – forrert Jan 26 '16 at 17:52
  • ok thanks for the reference – Riken Shah Jan 26 '16 at 18:01

1 Answers1

1

I'm not going to try turning this code into synchronous. I'll just correct it to work as an asynchronous version.

You need to pass in callback to wikitext() and return the value in that callback. Here is the revised code of wikitext() and the route that calls it:

var wikipedia = require("wikipedia-js");
module.exports = {
    wikitext: function(topicname, callback) {
        console.log("Inside wikitex funciton :" + topicname);
        var options = {
            query: topicname,
            format: "html",
            summaryOnly: false,
            lang: "en"
        };

        wikipedia.searchArticle(options, function(err, htmlWikiText) {
            console.log("Inside seararticlefunciton :");
            if (err) {
                console.log("An error occurred[query=%s, error=%s]", topicname, err);
                return callback(err);
            }
            console.log("Query successful[query=%s, html-formatted-wiki-text=%s]", topicname, htmlWikiText);
            callback(null, htmlWikiText);
        });
    }
};


router.get('/wiki/:topicname', function(req, res, next) {
    var topicname = req.params.topicname;
    console.log(topicname);
    summary.wikitext(topicname, function(err, result) {
        if (err) {
            return res.send(err);
        }
        if (!result) {
            return res.send('No article found');
        }
        res.send("Hello "+result);

    });
});
Ben
  • 5,024
  • 2
  • 18
  • 23