0

I'm struggling with the asynchronicity of javascript using https://github.com/segmentio/nightmare to save its scraped data to a variable.

var Nightmare = require('nightmare');
var nightmare = Nightmare();

var title = nightmare
    .goto('https://github.com')
    .evaluate(function () {
        return document.title;
    })
    .end()
    .then((result) => {
        // console.log(result);
    });

console.log(title);

The console output looks like: Promise { <pending> }

How do I store the result to the title variable before the console will log? Am I supposed to use generator functions with yield?

  • 1
    Just uncomment the line inside the `then` - you seem to already know how it works? – Bergi Jun 16 '16 at 07:36
  • If you read the linked answer, you will find out that you are trying to solve the wrong thing. You cannot return a value from an asynchronous call, so "how do I return a value from an asynchronous call" is focusing on the method, when you should be asking yourself "what do I want to do with this value", as it will only be reliably available in the callback. If the answer is "print it", as @Bergi says, you already know how to do it. – Amadan Jun 16 '16 at 07:58

0 Answers0