Legitimate uses of IIFEs?

Dmitry Soshnikov dmitry.soshnikov at gmail.com
Sat Dec 19 20:14:44 UTC 2015


On Saturday, December 19, 2015, Šime Vidas <sime.vidas at gmail.com> wrote:

> With block statements + let/const, IIFEs are no longer needed to emulate
> block-scoped variables. That got me thinking, are there other uses of
> IIFEs, or are they no longer needed?
>
> I’ve checked my code and found instances of this pattern:
>
> var foo = (function () {
>     var a, b, c; // helper variables
>     // some computation
>     return /* final value of foo */;
> }());
>
> Btw, there is a "do expression" proposal (stage 0) [1] for this type of
> pattern.
>
> Anything else?
>

FWIW, one of the still valid use cases is async IIFE, to spawn an async
code (since there's no yet top-level async/await)

(async function() {
  // await here
})();

Dmitry
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151219/0a47a2e1/attachment-0001.html>


More information about the es-discuss mailing list