DEV Community

Discussion on: A few handy JavaScript tricks

Collapse
 
morganconrad profile image
Morgan Conrad

I seldom use IIFEs, but I like your idea of prefacing with void. I typically name the function "iife" as well. (or iife1. iife2...)

Collapse
 
noseratio profile image
Andrew Nosenko

I find myself using them more often for generators or async generators, like that getRacers from the article:

await Promise.race(function* getRacers() {
  for (const p of iterablePromises) {
    if (!p?.then) throw new TypeError();
      const settle = () => winner = winner ?? p;
      yield p.then(settle, settle);
    }
  }());
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dvdty profile image
Pavel

You can replace
winner = winner ?? p;
with
winner ??= p;

Thread Thread
 
noseratio profile image
Andrew Nosenko

A good point, thanks. Though, the logical nullish assignment is fairly new (Chrome 85+, Node 15+).