DEV Community

loading...

Discussion on: A few handy JavaScript tricks

Collapse
noseratio profile image
Andrew Nosenko Author

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 Author

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