0

I have an array of 500 links. I iterate through it using eachThen. Inside eachThen I add thenOpen to check link status but sometimes my thenOpen takes too much time and my entire application is idle.

Can I add timeout in eachThen so that if thenOpen takes too much time so I can skip that link and continue with another link in the array?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Suraj Dalvi
  • 988
  • 1
  • 20
  • 34

1 Answers1

1

Set a stepTimeout on constructor and store failed urls on onStepTimeout like:

var casper = require('casper').create({
    verbose: true,
    logLevel: "warning",
    waitTimeout: 2000,
    stepTimeout: 2000, //<-- adjust timeout
    viewportSize: {
        width: 800,
        height: 400
    },
    pageSettings: {
        "userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
        "loadImages": false,
        "loadPlugins": false,         
        "javascriptEnabled": true,         
        "webSecurityEnabled": false,
        "ignoreSslErrors": true
    },
    onWaitTimeout: function() {
        //throw new Error
    },
    onStepTimeout: function() {
        //add an entry with timeout url
        failed.push(this.requestUrl);
        this.echo("Url " + this.requestUrl +" timed out ");
        this.clear();
    }
});

var urls = [
    //some urls
];

casper.start().eachThen(urls, function(response) {
    this.thenOpen(response.data, function(response) {
        console.log('Opened', response.url);
    });
});

EDIT: This is out of question's scope but to abort a request I use:

casper.on('resource.requested', function(requestData, request) {
    //some filter?        
    if (requestData.url.indexOf('/json/') > -1) {
        request.abort();
    }
});
Jonatas Walker
  • 13,583
  • 5
  • 53
  • 82