A
A
Alexey2017-09-20 10:09:55
JavaScript
Alexey, 2017-09-20 10:09:55

Nightmare.js how to save the result to a variable?

var Nightmare = require('nightmare');
var nightmare = Nightmare({ show: true });

var fq = [];

nightmare
    .goto('---------------')

    .evaluate(() => document.querySelector('------------').innerText)
.end()
    .then(function(rez){
        fq = rez;
    })

    .catch((error) => {
    console.error('Search failed:', error);
});

console.log(fq);

Nightmare.js, as I understand it, works asynchronously.
Trying to perform this manipulation
.then(function(rez){
        fq = rez;

the last line will be executed first console.log(fq); in the main code with the result [], and only then Nightmare.js starts. How can I store the result of .evaluate into an array "fq"?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2017-09-20
@vitali1995

.then(function(rez){
        fq = rez;
        console.log(fq);
})

Otherwise, nothing, this is asynchrony.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question