Search code examples
javascriptpromisebluebirdcheerio

Promise Undefined instead of boolean


I'm using Promise library to get result of another promise-request library with cheerio request but instead of boolean I keep getting undefined

return Promise.try(function () {
        .....
    }).then(function () {
        return self.checkGroupJoined(id);
    }).then(function (data) {
        console.log(data);

and my method with promise-request

this.checkGroupJoined = function (steam_id) {
    var options = {
        uri: 'url',
        transform: function (body) {
            return cheerio.load(body);
        }
    };

    return rp(options).then(function ($) {
        $('.maincontent').filter(function () {
            if ($(this).find('a.linkTitle[href="url"]').length > 0){
                return true;
            } else {
                return false;
            }
        });
    }).catch(function (err) {
        return error.throw('Failed to parse body from response');
    });
};

Should I promisifyAll libraries?


Solution

  • You need to change this section

    return rp(options).then(function ($) {
            // You are not returning anything here
            $('.maincontent').filter(function () {
                if ($(this).find('a.linkTitle[href="url"]').length > 0){
                    return true;
                } else {
                    return false;
                }
            });
        }).catch(function (err) {
            return error.throw('Failed to parse body from response');
        });
    

    If you change your code to this it should work.

    return rp(options).then(function ($) {
            let found = false;
            $('.maincontent').filter(function () {
                if ($(this).find('a.linkTitle[href="url"]').length > 0){
                    found = true;
                }
            });
            return found;
        }).catch(function (err) {
            return error.throw('Failed to parse body from response');
        });