I am trying to return an number (expensePerProduct), which I will use to "PUT" the data in to an object, but this function returns a promise object and not just a number :( What do I do here?
This is my code:
const calculateExpense = async function (proName) {
        let expensePerProduct = 0;
        fetch('/purchase')
            .then(result => {
                if (result.status >= 400) {
                    throw new Error(result.status);
                } else {
                    return result.json();
                }
            })
            .then(result => {
                let totalExpense = 0;
                let totalQuantity = 0;
                for (let purchase of result) {
                    if (proName == purchase.productName) {
                        totalExpense += purchase.totalPrice;
                        totalQuantity += purchase.purchasedQuantity;
                    }
                }
                expensePerProduct = totalExpense / totalQuantity;
                return expensePerProduct;
            })
            .catch(err => console.log("Error: " + err));
    }
I am new to stackoverflow (and to JS), so tell me if you need more information..
 
     
     
    