3

So I have this file:

 'use strict'
import * as moment from "moment";
import { Report} from "./Report";
import { Timeframe} from "./Timeframe";
import { ReportComparison } from "./ReportComparison";

function test(firstFrom: string, firstTo: string, secondFrom: string, secondTo: string) {


    var pastReport = new Report(new Timeframe(moment(firstFrom), moment(firstTo)));
    var laterReport = new Report(new Timeframe(moment(secondFrom), moment(secondTo)));

    var reportComparison

    function getData(pastReport: Report, laterReport: Report) {

        var later = function() {
            return new Promise((resolve, reject) => {
                laterReport.fetchData(data => resolve(data));
            });
        };

        var past = function() {
            return new Promise((resolve, reject) => {
                pastReport.fetchData(data => resolve(data));
            });
        };

        return Promise.all([later(), past()]).then(() => {
            laterReport.sort();
            reportComparison = new ReportComparison(pastReport, laterReport);

            return {
                pastReport: {
                    projects: reportComparison.pastReport.projects,
                    timeFrame: reportComparison.pastReport.timeframe,
                    totalAutomatedRuns: reportComparison.pastReport.totalAutomatedRuns,
                    totalManualRuns: reportComparison.pastReport.totalManualRuns

                },
                laterReport: {
                    projects: reportComparison.laterReport.projects,
                    timeFrame: reportComparison.laterReport.timeframe,
                    totalAutomatedRuns: reportComparison.laterReport.totalAutomatedRuns,
                    totalManualRuns: reportComparison.laterReport.totalManualRuns
                },
                trends: reportComparison.trends
            }

        });

    }

    return getData(pastReport, laterReport).then((res) => {
        return res;
    });

}



console.log(test("20170707", "20170707", "20170710", "20170710"))

Right now, the console.log returns Promise { <pending> } and not the actual value I want returned (the JSON object). Am I approaching this asynchronous code correctly? What solution could I use to return the JSON as a value, not a pending promise.

Adam Siwiec
  • 923
  • 2
  • 9
  • 21

2 Answers2

4

Yes, You will get promise only as you are returning promise from here

return getData(pastReport, laterReport).then((res) => {

    return res; //Do callback here...
});

This is your main return I mean return value of test function and you have returned promise only and note that what you have returned from then it will be returned in that function only so it will not reflect in test's return value. If you don't want to change your code you can use callback

or change it to

return getData(pastReport, laterReport)

and make your test call like this

test(...).then((res)=>{ 
})
Or like this
var a = await test(...)
then test will return data.
Yash Ganatra
  • 468
  • 2
  • 12
3

The method test indeed returns a promise. To access the data, call the .then() function.

test("20170707", "20170707", "20170710", "20170710")
    .then((response) => {
         console.log(response);
    });
JSON Derulo
  • 9,780
  • 7
  • 39
  • 56