O
O
odd-look2015-02-22 13:59:08
JavaScript
odd-look, 2015-02-22 13:59:08

Can a callback return multiple parameters?

I am writing a parser, this is a function that counts the number of pages

var request = require('request');
var cheerio = require('cheerio');

var counter = function (link, i, callback) {
    request(link + i, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var $ = cheerio.load(body);
            if ($('.b-products__list .main-bigger3').text().trim() == 'К сожалению, в данной категории нет товаров') {
                callback(link ,--i);
            }
            else {
                counter(link, ++i, callback);
            }
        }
    })
};

module.exports = counter;

Unfortunately it doesn't work
Error: Can't set headers after they are sent.

But if you return only i, then everything is fine.
Is there a way to return the link as well?
PS callback must be used.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Shemsedinov, 2015-02-22
@odd-look

You can pass any number of parameters to the callback, but it is customary to make the first parameter error. The error is in the code from which you call this module. One can only guess, but most likely, the top-level code somehow checks the first parameter of the callback, try passing null as the first parameter, or give the code to the studio.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question