¿Para qué exactamente deberías hacer una función? fetchStockHistory() es una de las funciones principales de mi aplicación. ¿Cómo sé qué debe tener su propia función? Por ejemplo, ¿hago una función solo para obtener los datos del formulario, y otra para obtener al día siguiente, y otra solo para buscar, etc., luego llamo a todas estas funciones desde fetchStockHistory() ? Ahora estoy pensando que fetchStockHistory() debería simplemente devolver una fetch .
En otras palabras, pregunto cómo saber cuándo dividir una función en funciones más pequeñas para llamar desde la función más grande, en este caso fetchStockHistory() .
const fetchStockHistory = async (event) => { try { event.preventDefault(); // Clear error messages document.getElementById('message').textContent = ''; // Get form props const formData = new FormData(event.target); const formProps = Object.fromEntries(formData); let endDate = formProps.endDate; // 2000-05-01 // Get the next day - otherwise the fetch will end the day prior if (endDate) { var nextDay = new Date(endDate); // May 01 2000 nextDay.setDate(nextDay.getDate() + 1); // May 02 2000 // Convert to yyyy-mm-dd endDate = nextDay.toLocaleDateString('en-CA') // 2000-05-02 } const response = await fetch('https://api.twelvedata.com/time_series?&start_date=' + formProps.startDate + '&end_date=' + endDate + '&symbol=' + formProps.pickStock + '&interval=1day&apikey=**********************') if (!response.ok) throw Error('Did not recieve expected data') const historyData = await response.json(); // Set message if (historyData.message) { ...(rest of code)