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.