These are chat archives for rosshinkley/nightmare

15th
Jul 2016
Federico Marcos
@marcosfede
Jul 15 2016 04:00
@rosshinkley this link https://github.com/rosshinkley/nightmare-examples/blob/master/docs/common-pitfalls/async-operations-loops.md was very helpful, thanks. I’m almost finishing debugging and I want to add an end() so the electron process finishes and I can continue processing the data in my node script. where can I do that? I’m doing a loop over an array of links and extracting data from each one, I want to end the electron process once all of them have been parsed. I can share a little code if that helps. thanks!
Ross Hinkley
@rosshinkley
Jul 15 2016 04:01
@marcosfede no problem :) happy to help
it depends on what approach you're using
if you're using co or vo, you should be able to yield nightmare.end(); at the end
Federico Marcos
@marcosfede
Jul 15 2016 04:03
I’m using vanillaJS, chaining with reduce
Ross Hinkley
@rosshinkley
Jul 15 2016 04:03
gotcha
so, at the end, you've probably got a .then() that runs after .reduce() right?
Federico Marcos
@marcosfede
Jul 15 2016 04:04
yes
Ross Hinkley
@rosshinkley
Jul 15 2016 04:04
you should be able to do something like...
//pretend reducePromise is the promise result of the reduction
reducePromise.then(() => {
  //... do some things, cleanup, etc...
  return nightmare.end();
})
.then(() => {
  console.log('nightmare ended successfully');
});
(there are other ways to tackle this, but that should get you started)
Federico Marcos
@marcosfede
Jul 15 2016 04:12
The thing is that I need to return some processed data at that point, I have tried calling nightmare.end() inside that reducepromise.then() and return the value I want to keep instead, but the process is not closing
how would I close it and have some data available in the next .then((data) => … )
Ross Hinkley
@rosshinkley
Jul 15 2016 04:13
ah, i see
off the cuff...
i think you could have a promise chain inside of the reduce promise .then(), and return the results from that
liiiike...
reducePromise.then((results) => {
  return nightmare.end().then(() => results);
})
.then((results) => {
  console.log('nightmare ended successfully and you have results');
});
Federico Marcos
@marcosfede
Jul 15 2016 04:17
wow that worked beautifully, thanks a lot :) !
Ross Hinkley
@rosshinkley
Jul 15 2016 04:17
:) no problem
bersam
@Bersam
Jul 15 2016 15:20
Hi everybody! I have a problem with chaining evaluate process with cookies.get
nightmare.goto("url")
      .evaluate(function () {
        return document.querySelector('body');
      })
      .then(function(result){
        log.info(result, result);
      })
      .cookies.get()
      .then(function(result) {
        // do something with cookies
       }
both functions works without a problem separately, but seems "then" doesn't return nightmare object to next chain.
Kenta Iwasaki
@iwasaki-kenta
Jul 15 2016 15:22
cookies.get() can be called anytime through nightmare.cookies.get()
It's not a part of the method chaining portion of NightmareJS
So you can just have a singular .then(function(result) { log.info(result, result); log.info(nightmare.cookies.get()); });
bersam
@Bersam
Jul 15 2016 15:24
Aha! so nightmare keeps its state at each step.
thanks ;)
Kenta Iwasaki
@iwasaki-kenta
Jul 15 2016 15:24
Yep :), no problem.
rosshinkley @rosshinkley pokes head in
Ross Hinkley
@rosshinkley
Jul 15 2016 15:27
.then(function(result) { log.info(result, result); log.info(nightmare.cookies.get()); });<- i don't think this will do what you'd expect
Kenta Iwasaki
@iwasaki-kenta
Jul 15 2016 15:28
Oh, if you want to do something with the cookies other than grab it after some actions then my bad
Ross Hinkley
@rosshinkley
Jul 15 2016 15:28
but @Dranithix is right: the return of Nightmare.then() is a native promise, not a nightmare instance, so chaining nightmare methods off of it won't work
something like...
nightmare.goto("url")
      .evaluate(function () {
        return document.querySelector('body');
      })
      .then(function(result){
        log.info(result, result);
        return nightmare.cookies.get();
      })
      .then(function(result) {
        // do something with cookies
       }
bersam
@Bersam
Jul 15 2016 15:29
// promise hell
.then(function(result) {
log.info(result, result);
nightmare.cookies.get().then(function(cookies){
// cookies
})
});
Ahaaa! great ;)
Ross Hinkley
@rosshinkley
Jul 15 2016 15:30
yeah, you're on the right track @Bersam
Kenta Iwasaki
@iwasaki-kenta
Jul 15 2016 15:45
@rosshinkley Don't know if you're there, but check PM please
Ross Hinkley
@rosshinkley
Jul 15 2016 15:46
oops, sorry
Kenta Iwasaki
@iwasaki-kenta
Jul 15 2016 15:46
It's nothin' big xD