Refresh form problem

i have a problem ill try to compute the number of days between posting date and due date. when i save the form i created the result will not show but when ill try to refresh the form it will show the answer.

i think i have a problem with my code.

$.each(cur_frm.doc.references || [], function(i, d) {
    frappe.db.get_value(d.reference_doctype,d.reference_name,"due_date", function(value){
        var diff_day = frappe.datetime.get_diff(value["due_date"], cur_frm.doc.posting_date);
        frappe.model.set_value(d.doctype, d.name, "no_of_days_fr_delivery", diff_day);
     });                    
});

You can refresh form by cur_frm.reload_doc()

hi @magic-overflow thankyou for your help. the problem still the same

I have created a site to test this script and it works. Once the payment entry saved, no of day fr delivery is calculated.
Here’s the link:
https://codrider.erpnext.com

I will message you the user and password.