Capture error return in asynchronous function for treatment

1

I am trying to handle a timeOut error. When the timeOut occurred the application should run again. Initially this loop should be done in an external module that called 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();
}
    
asked by anonymous 11.03.2017 / 20:11

0 answers