0

I am using an ajax to get some of my data from database without refreshing my page. So made this function:

function setElapsedDays(contract){
    ...
    let diff_in_time = end_date.getTime() - start_date.getTime();
    let days_elapsed = diff_in_time / (1000 * 3600 * 24);

    console.log(this.getVacationEntitlment(contract.id));
    let accrued = this.getVacationEntitlment(contract.id) / days_elapsed;
    console.log(accrued);
}

async function getTravelEntitlment(contract_id){
    let url = window.location;

    const response = await fetch(url.origin+"/api/benefits/search/2/"+contract_id);
    const object = await response.json();
    return object.allotment;
}

but why i am getting NaN on my accrued? Using console.log(this.getVacationEntitlment(contract.id)); I am getting

Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: 30

If i console.log() my object i get this {id: 2, contract_id: 736, benefit_id: 2, allotment: 30, uom: "Days", …}

How can i get the value of PromiseValue?

Anirudh Lou
  • 781
  • 2
  • 10
  • 28
  • 1
    you are not `await`ing for your `getTravelEntitlment()` function to be over before `console.log`ging it. – Nicolas Nov 25 '19 at 13:47
  • How should i await for it? I am sorry but i am not that much familiar with javascript. – Anirudh Lou Nov 25 '19 at 13:50
  • 1
    try adding an `await` keyword before you call `this.getVacationEntitlment(contract.id`. Something like `let accrued = await this.getVacationEntitlment(contract.id) / days_elapsed;` – Nicolas Nov 25 '19 at 13:52

0 Answers0