Ad

CasperJS Each Then Next (future) Value Of Response Array

- 1 answer

im using eachThen() method of CasperJS and I need to know the value of the next iteration item.

I have items like this :

array = [

[22,"blah"],
[22,"blah"],
[23,"blah"],
[24,"blah"],
[24,"blah"],
[24,"blah"]
]

I'm generating one file per group of items (grouped by [0]) .

My first approach was to compare the actual item with the previous item at the beginning of the iteration , and if [0] is different generate the file, like this :

//previousItem was initilizated = to actualItem

   casper.eachThen(array , function (response) {
            var actualItem = response.data

        casper.then(function(){
            if(actualItem[0] != previousItem[0]){
                var filename = previousItem[0]+"file.html"
                var f = fs.open(filename, 'a');
                f.write(stdFile);
                f.close();  
                previous = actualItem
            }
        });

//operations that prepares stdFile to be created

Problem appears at the end of the cycle, as im comparing actual with previous, the last group will not generate any files (of course, the 2 last items will have the same [0], and after that the cycle will end)

My solution here is to ask for the next item instead of the previous, and generate the file at the end of each iteration , but I dont know how to tell Casper to give me the item of actualItem+1 in the array.

I will try to fix this iterating the same array inside in paralel and returning the value of actual+1 , but maybe there is a way to do it using response variable.

The result must be 3 files : 22file.html , 23file.html and 24file.html There are a lot of requests to make

, so I need to win any second I can.

If you have another approach to achieve this please let me know.

PS: Sorry for my english, its not my native language

Thanks

Ad

Answer

My first approach was too complicated, probably because I didnt sleep so much these days.

This is what I did :

cajIndex = 0;

//this block generates the file if actualItem (or actualCajero is the same) is not undefined  and the actualItem[0] is not the same as the next item 

if (actualCajero != undefined)
        {
            if (actualCajero[0] != cajerosArray[cajIndex][0]){
                casper.then(function(){

                    var filename = "Ventas local"+" "+nombreLocal+"_"+actualDay[1]+"-"+actualDay[2]
                    stdFile = '<h1>'+filename+'</h1> <table border="1">'+ stdTable + '</table>';
                    this.echo("generando archivo "+filename);
                    var f = fs.open(filename+".xls", 'a');
                    f.write(stdFile);
                    f.close();  
                    stdTable = "";
                    stdFile = "";

                });
            }
        }

//after this i do my operations , ask again for file creating at the end but now if the next item is undefined, if it is undefined creates the file then

cajIndex++

and that solves everything, the key was declaring that index at 0 and asking for file creating before assigning actualItem to response, and then ask again at the end for the last item.

Hope it helps

Ad
source: stackoverflow.com
Ad