DEV Community

Discussion on: Explain IIFE(Immediately Invoke Function Expression) Like I'm Five

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

A more recent use of iife is to use async await.

;(async () => {
   const myDataResponse = await fetchData("https://example.com/some-api.json");
})();

The above changes the context of the block (the function body) to mean I can use await to grab data that may take some time to retrieve.

Collapse
 
codefinity profile image
Manav Misra

twitter.com/GoCodeFinity/status/12... This is great, as I just was wondering (via Twitter b4 seeing this) about my pattern that is very similar to yours.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

It's not wired at all, as your tweet asks. It's just a workaround until top-level await comes in circa 2020. In the mean time I always include a semicolon Infront so the interpreter doesn't think that any preceding thing is a function call. You should do the same. 👽

Thread Thread
 
codefinity profile image
Manav Misra

Tx. 'weird' not 'wired' 👆🏽;)

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀

I use a phone exclusively on Dev.to