Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
What exactly should you make a function for?

What exactly should you make a function for? fetchStockHistory() is one of the main functions of my app. How do I know what should have it's own function? Like, do I make a function just to get the form data, and one to get the next day, and one just to fetch, etc, then call all these functions from fetchStockHistory()? Now I'm thinking fetchStockHistory() should just return a fetch?

In otherwords, I'm asking how to know when to break a function up into smaller functions to call from the bigger function, in this case 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)
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda