How to call a function inside a function from outside of it?

Viewed 626

I have a function that performs fetch and processes this data:

async function fetchData(){
    const res = await fetch("./data.json");
    const data = await res.json();

    // processing one-time code

    function doSome() {
    // code that looks for something in data and processes it(I'm going to call this function many times)
    }
    doSome()
}
fetchData();

I can call the doSome function inside fetchData, but I need to call doSome outside fetchData.

How can I not process one-time code and only run doSome?

4 Answers
function doSome(data) {
// code that looks for something in data and processes it(I'm going to call this function many times)
}

async function fetchData(){
    const res = await fetch("./data.json");
    const data = await res.json();

    // processing one-time code
    return data;
}

let data;

fetchData().then(fetchResult => {
    //Stuff you want to do once you have the data.
    data = fetchResult;
    doSome(data);
    // Handle any queued events.
});

// Pseudo-code event handler you can attach before data is ready 
const eventHandler = (event) => { 
    if (!data) { 
        // Pseudo-code denoting a function that queues an event to be executed later
        queueEvent(event) 
    } else { 
        doSome(data)
    }
}

So I removed doSome from the scope of fetchData into the module's scope. Then I changed fetchData to return the data, so we can use that result later on. Once it resolves, it sets variable data, and you can re-use it, and do any required actions in the then callback. Depending on your needs, you can either add event listeners there, or queue events and fire the handlers requiring data once it resolves. Hope this helps!

The inner function scope is confined to the outer function. Therefore it cannot be called from outside, similarly to a variable declared from inside a function.

It seems you have two independent actions where the output of one is the input of the other. So, do two separate functions and you get cleaner code.

async function fetchData(){
    const res = await fetch("./data.json");
    const data = await res.json();

    return data;
}
const json = fetchData();


function doSome(json) {
    // code that looks for something in data and processes it(I'm going to call this 
     function many times)
}

doSome(json);
doSome(json);
doSome(json);
doSome(json);

You can always encapsulate them in a JSON/Object if you want to ship them around together. Play with exporting just the right methods and keep a global (to this file) variable holding the JSON.

    let json = {};
    async function fetchData(){
        const res = await fetch("./data.json");
        const data = await res.json();
        json = data;
    }


  function doSome() {
  console.log(json);
        // code that looks for something in data and processes it(I'm going to call this 
         function many times)
    }

   //You probably want to call this from the outside, not in this file.
   doSome();
   doSome();
   fetchData();
   doSome();
   doSome();

   //not sure what is your functionality, so not sure what you would need to export here
   export default {
       fetchData,
       doSome
   }

Make doSome a closure and return it from the method.

async function fetchData() {
  // do your fetch logic
  const doSome = () => {
    // logic
  };

  return doSome;
}

const doSome = await fetchData();
doSome();

The closure will make sure you retain proper context of the wrapping method it was declared in, ensuring it can access the properties declared in the fetchData method context.

Related