A return value is the explicit output produced by a function. A caller can use it for another calculation, pass it to an action, or choose how to present it. Returning the answer instead of writing directly to the DOM makes a function reusable for interfaces, accounting, and shipping. section 4.5 section 4.7
Use return values to separate calculation from action:
- Replace implicit inputs with arguments and implicit outputs with return values. This turns an action into a calculation that is easier to organize and reuse. section 4.7 section 5.14
- With synchronous code, return the computed value and perform the consuming action one level up the call stack. section 15.30
- With asynchronous code, a normal return happens before the result exists; pass a callback and call it later with the result. section 15.29 section 15.30
Returning an asynchronous total
A synchronous calculation can return its total directly. An asynchronous calculation instead accepts a callback: calc_cart_total(cart, callback) computes the cost and shipping, then calls callback(total). The caller can provide update_total_dom or another consumer, such as accounting. section 15.29 section 15.30







