Legitimate uses of IIFEs?

Mat At Bread bread at mailed.me.uk
Sat Dec 19 22:01:46 UTC 2015


Which is what I said, I hope. Use the .then for top-level invitation. 
Dimitry's example wouldn't resolve the Promise


On 19 December 2015 9:24:04 pm Fabrício Matté <ultcombo at gmail.com> wrote:

> @bread I see you are referencing Dmitry's sample, but why do you say it
> won't work? AFAIK async functions return promises, so you don't necessarily
> need a top-level `await`. I believe this (extremely ugly) sample should
> work:
>
> ```js
> function f(cb) {
>   (async function() {
>     // await here
>   })().then(v => cb(null, v), cb);
> }
> ```
>
> /fm
>
> On Sat, Dec 19, 2015 at 7:11 PM, <bread at mailed.me.uk> wrote:
>
>> That’s not going to work. The correct form still requires an (illegal)
>> top-level await:
>>
>> await (async function() {
>> // await here
>> })();
>>
>> The easiest way to spawn a top-level async function is:
>>
>> here.then(function(result){},function(error){}) ;
>>
>> On 19 December 2015 20:14:44 -00:00, Dmitry Soshnikov <
>> dmitry.soshnikov at gmail.com> wrote:
>>
>>
>>
>> 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
>>
>>
>>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151219/c549de8e/attachment-0001.html>


More information about the es-discuss mailing list