Use Nightmare.js without ES6 syntax and productivity

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); // notice that `throw`ing in here doesn't work }); 
+7
javascript yield ecmascript-6 nightmare
source share
1 answer

The docs are terrible, but Nightmare seems to be based on thenables. I also did not find much information about the callback interface, but this will still lead to an indented pyramid.

So, it is best to use promises, just select any library that roughly conforms to the ES6 standard (they can all be used in non-ES6 environments as well).

You can easily convert your linear generator code into a promise chain, just replace each yield with a call to then :

 var Nightmare = require('nightmare'); var Promise = require('…'); 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); // notice that `throw`ing in here doesn't work }); 
+12
source share

All Articles