0

I am using browser-perf to get the results of browser performance.

i have the following code:

var browserPerf = require('browser-perf');
var _ = require('lodash');
var fs = require('fs');
var colors = require('colors');
var couchbase = require('couchbase');
const Promise = require('bluebird');
var performanceMetricsDriver = {

    recordPerfMetrics: function(url) {
        var self = this;
        var perf, loadTime, domInteractive, firstPaint;
        var perfData = {};       
        fs.readFile('urls.txt', 'UTF-8', function (err,urls) {
            if (err) {
                return console.log(err);
            }

            var urls = urls.split("\n");
            urls.shift();

            urls.forEach(function(url) {     
                console.log(url);   
                self.getStats(url);
            });      

            // console.log(colors.magenta("Starting to record performance metrics for " + url));
            // this.storePerfMetrics();                       
        });    
    },

    getData: function(url) {
        return new Promise(function (resolve, reject) {
                console.log("NOW GETTING DATA FOR URL: " + url);
               // if (err) {
               //     Promise.reject("This erred out!!");
               // } else {
                //     Promise.resolve(data);
                //     console.log(data);
                //     loadTime = (data.loadEventEnd - data.navigationStart)/1000 + ' sec';
                //     firstPaint = data.firstPaint;
                //     domInteractive = (data.domInteractive - data.navigationStart)/1000 + ' sec';

                //     perfData = {
                //         'URL' : url,
                //         'firstPaint' : firstPaint,
                //         'loadTime' : loadTime,
                //         'domInteractive' : domInteractive
                //     };
                // }
            }).then(function(data, err) {

            });
    },

    getStats: function(url) {
        var self = this;

            browserPerf(url, self.getData(url), {
                selenium: 'http://localhost:4444/wd/hub',
                browsers: ['chrome'] 
            });
    } 
}

I am trying to capture the data being returned by browser-perf, but for some reason this keeps on erring out and gives me an unhandled rejection error

Kevin Yan
  • 1,236
  • 11
  • 19
pj013
  • 1,393
  • 1
  • 10
  • 28
  • You apply a `.then()` handler to the promise and you use the data INSIDE the `.then()` handler. That's how async results work. Possible duplicate of: [How do I return the response from an asynchronous call](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – jfriend00 Oct 24 '16 at 02:41
  • @jfriend00: I tried doing that, just not too sure where that would be handled in the above case. – pj013 Oct 24 '16 at 02:44

1 Answers1

0

You can only use the data returned by a promise inside the .then() handler. I don't know exactly what you're trying to do, but here's one idea:

getStats: function(url) {
    this.getData(url).then(function(data) {
        browserPerf(url, data, {
            selenium: 'http://localhost:4444/wd/hub',
            browsers: ['chrome']
        });
    });
}

This assumes that .getData() returns a promise that will eventually get resolved with your data (something your code does not show).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • i have a better explanation here as to what i am trying to do: http://stackoverflow.com/questions/40229948/retrieve-data-from-a-callback-thats-in-a-promise – pj013 Oct 25 '16 at 01:22
  • @jp310 - If this is still the same issue, why did you make a new question rather than just edit your current question to clarify? – jfriend00 Oct 25 '16 at 01:24