I built a simple node script using nightmare.js to clear websites.
var Nightmare = require('nightmare'); var vo = require('vo'); vo(run)(function(err, result) { if (err) throw err; }); function *run() { var x = Date.now(); var nightmare = Nightmare(); var html = yield nightmare .goto('http://google.com') .evaluate(function() { return document.getElementsByTagName('html')[0].innerHTML; }); console.log("done in " + (Date.now()-x) + "ms"); console.log("result", html); yield nightmare.end(); }
I want to run this in an environment using an older version of node that does not support ES6 features. There are no examples on the github page of how to do this without the "yield" keyword.
I found a usage example without ES6 syntax here: Webcraping with a nightmare
I wrote it like this:
var night = new Nightmare() .goto('http://www.google.com') .evaluate(function () { return document.getElementsByTagName('html')[0].innerHTML; },function (html) { console.log("result", html); } ) .run(function (err, nightmare) { if (err) return console.log(err); console.log('Done!'); });
This is not a failure, but the function of registering results is never called.
With yield syntax, getting the return value from the "estimate" is pretty simple , but without it, I could not find a way to do this.
UPDATE Wrote this thanks to the accepted answer and its comments. It uses "Q" and works in node versions prior to 0.12:
var Nightmare = require('nightmare'); var Promise = require('q').Promise; var x = Date.now(); var nightmare = Nightmare(); Promise.resolve(nightmare .goto('http://google.com') .evaluate(function() { return document.getElementsByTagName('html')[0].innerHTML; })).then(function(html) { console.log("done in " + (Date.now()-x) + "ms"); console.log("result", html); return nightmare.end(); }).then(function(result) { }, function(err) { console.error(err);
Rayjax
source share