Since Nightmare then is capable, you can return it from .then() to bind it as if you were a regular Promises.
var Nightmare = require('nightmare'); var nightmare = Nightmare({ show: true, paths: { userData: '/dev/null' } }); nightmare .goto('http://www.example.com/') .wait('h1') .evaluate(function() { return document.querySelector('title') .innerText; }) .then(function(title) { if (title == 'someTitle') { return nightmare.goto('http://www.yahoo.com'); } else { return nightmare.goto('http://w3c.org'); } }) .then(function() {
If you need more synchronous logic, you might consider using generators with vo or co . For example, the above is rewritten using vo :
var Nightmare = require('nightmare'); var vo = require('vo'); vo(function * () { var nightmare = Nightmare({ show: true, paths: { userData: '/dev/null' } }); var title = yield nightmare .goto('http://www.example.com/') .wait('h1') .evaluate(function() { return document.querySelector('title') .innerText; }); if (title == 'someTitle') { yield nightmare.goto('http://www.yahoo.com'); } else { yield nightmare.goto('http://w3c.org'); }
source share