Here is the content of my 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"))
In its current state, when I run console.log it displays Promise { <pending> }
instead of the desired JSON object. I am wondering if my approach to dealing with asynchronous code is correct? Can anyone suggest a solution that would allow me to return the JSON object directly rather than a pending promise?