Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning data from callback #370

Open
Anan5a opened this issue Dec 16, 2020 · 2 comments
Open

Returning data from callback #370

Anan5a opened this issue Dec 16, 2020 · 2 comments

Comments

@Anan5a
Copy link

Anan5a commented Dec 16, 2020

How can I return the response body from callback function? I'm unable to do anything and practically locked inside the callback only!

const crawler = require('crawler');

module.exports = {
    crawler:function (url){
        let result = null;
        let c = new crawler({
            maxConnections: 1,
            callback: function (error, response, done) {
                if (error) {
                    return console.error(error)
                }
                result = JSON.parse(response.body)
                //returning result from crawler() ??
                done()
            }
        });
        c.queue(url)
        return result
    },

}
@Verhov
Copy link

Verhov commented Dec 19, 2020

During using module architecture you free to organize your code as various of ways, it's not node-crawler package question, but anyway in you case e.g. just pass callback function (or Promise better):

const crawler = require('crawler');

module.exports = {
    crawler:function (url, **callback**) {
    ...
       result = JSON.parse(response.body)
       **callback && callback(result)**
    ...

in your-crawler.js

const myCrawler = require('./my-crawler');

myCrawler.crawler('https://google.com', (result) => {
    console.log(result)
})

@devarsh10
Copy link

if(!error){
res.send(data);
}
else{
console.log(error);
}

In short, res.send might be helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants