Capture error return in asynchronous function for treatment

Asked

Viewed 271 times

1

I am trying to treat a timeout error. When the timeout occurs the application should run again. Initially this loop should be done in an external module that calls this, however due to the difficulties I ended up trying to treat both in the same file.

Currently the code is as follows:

exports.acesso = function(sitepage, selector) {

    //variaveis
    const now = new Date,
        fs = require('fs'),
        Nightmare = require('nightmare'),
        nightmare = Nightmare({
            show: false,
            waitTimeout: 2
        });
    var date, result = {};

    //processo
    var processo = function() {
        nightmare.goto(sitepage)
            .wait(selector)
            .evaluate(function(selector) {
                return document.querySelector(selector).innerText;
            })
            .end()
            .then(function(result) {
                console.log(result);
            })
            .catch(function(error) {
                if (error.message === '.wait() timed out after 2msec') {
                    console.error('Error: 500');
                    console.log('tentando novamente');
                    //aqui deveria ocorrer o loop
                    //tentei com processo();
                } else {
                    console.error(error);
                    return error;
                }
            })
    }
    processo();
}
  • There was some mistake when you used processo(); within the catch?

  • No, it closes the application, but does not loop

  • Maybe Promise chaining is failing... Take a look here: https://jsfiddle.net/c8rsddrf/, adapting to your case you would have to do return nightmare.goto(sitepage) ... etc and then inside the catch when you call again the process do return processo();. Test that.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.