javascriptasync-awaitself-invoking-function

Can Self Invoking Async function returns public functions


I'm actually new to JavaScript. So here's my question.

From what I learned. I can create a public function (getHello) inside a self invoke function (Example1) & call the created public function from another self invoke function (Example2) like shown below:-

// first self-invoke function
const Example1 = (() => {
    // daclare var to hold Hello World
    let hello = 'Hello World'

    return {
        // create public function to passed 'Hello World'
        getHello: () => {    
            return hello
        }
    }
})();

// second self-invoke function - receive 1 argument (Example1 function)
const Example2 = ((e1) => {
    // declare new var
    let newHello = e1.getHello()
    console.log(newHello) // will Output: Hello World

})(Example1);

I have tried the above code and it works wonder. Never knew that JavaScript could be so much fun! This means that I can basically share any private 'data' inside Example1 to any other self-invoke functions as long as I create a public function to enable the data sharing.

Anyway, with that in mind. I thought why not I create a dedicated self-invoking function to handle any data fetch from an API. So to do that I need to put async at the self invoke function in order to use await for fetching json data (as shown below)

// first async self-invoke function
const Example1 = (async() => {
    // get data from API fetch
    let res = await fetch(API_URL)
    let json = await res.json()
    let hello = json

    return {
        // create public function to passed 'Hello World'
        getHello: () => {    
            return hello
        }
    }
})();

// second self-invoke function - receive 1 argument (Example1 function)
const Example2 = ((e1) => {
    // declare new var
    let newHello = e1.getHello() // error occurs here
    console.log(newHello)

})(Example1);

But unfortunately, by doing so. It gave me this error saying "e1.getHello is not a function".

I have tried searching the answer or any explanation on google. But I can't seem to find any relevant topic discussing regarding what i just illustrate here.

So the questions would be;-

1) Can an async self-invoke function returns a public function at all? or I shouldn't or not recommended to do this at all?

2) If can/cannot, then why?

Any help would be appreciated!


Solution

  • async function returns Promise object. In the given example, Example1 will be a Promise object, not pure object.

    So if you want to use it, you have to use then or await to get the value in the promise.

    This will work as expected:

    const Example1 = (async() => {
        let res = await fetch(API_URL)
        let json = await res.json()
        let hello = json
    
        return {
            getHello: () => {    
                return hello
            }
        }
    })();
    
    const Example2 = (async (e1) => {
        el = await e1; // **** THIS IS KEY POINT ****
        let newHello = e1.getHello()
        console.log(newHello)
    })(Example1);