0
votes

I have an array of links, which I get in first request. My goal is to go to every link to gather data. So I want to make a promise for every request, push them all into an array and then pass to Q.all to resolve all the promises. The problem is I can't return promise and go to the next link Here is the function, where I tried to make multiple requests and gather data

function arrayPromise(linksArr){

function collectingData(elem){
    var deferredNew = Q.defer();
    var url = elem;
    request(url, function(error,response,html){
        if(error){
            deferredNew.reject(error);
        }
        var $ = cheerio.load(html);
        var title, content;

        $('.entry-title').filter(function(){
            var data = $(this);
            var title = data.text();
            items.text.push(
                {  titleof: title }
            )
        })

        $('.entry-content ').filter(function(){
            var data = $(this);
            var content = data.html();
            items.text.push(
                { contentof: content})
        })
        deferredNew.resolve(items);
    })
    console.log("Returning the promise");
    return defferedNew.promise;
}

var promiseArr;
console.log("LENGTH:");
console.log(linksArr.length);

for (var i = 0; i < linksArr.length; i++) {

    console.log(linksArr[i]);

    var tempPromise = collectingData(linksArr[i]);
    console.log(tempPromise);
    promiseArr.push(tempPromise);

};
    return promiseArr;
}

And how I try to use it

var linksPromise = fetchLinks();
linksPromise.then(function(arr){
    console.log("LINKS PROMISE RESOLVED");

    Q.all(arrayPromise(arr)).then(function(data){
        console.log("SUCCESS RESOLVING ALL PROMISES")
        console.log(data);
    },function(err){
        console.log("ERROR RESOLVING ALL PROMISES", err);
    });
},function(err){
    console.log(err);
})
2
then pass to Q.all to resolve all the promises - I think you misunderstand the function of Q.all. It doesn't resolve the promises passed in, it resolves when all the promises in the passed in array resolve, or rejects when any one of those promises rejects - Jaromanda X
I understand that. I think error may not be in Q.all, but rather in function that generates array of promises - Alyona

2 Answers

0
votes

promiseArr should be declared as an array:

var promiseArr = [];

If that doesn't fix it, please provide the error that you might be seeing.

0
votes

There are MULTIPLE problems
First is in

    deferredNew.resolve(items);

items is defined in a local scopes not defined anywhere in scope where deferredNew.resolve(items); evaluated.

Another: Assigning empty array to promiseArr would help too.

One more: request(url, function(error,response,html) is not assigning result anywhere and your function has no return statement where you think you return promice deferredNew.resolve(items);

PS There are more erros, check that all your function return value, for example $('..').filter(...) does not reurn values