Vai al contenuto principale
โ† โšก JavaScript & the browserยทModulo C6 ยท Lezione 11
CompitoQuick recap. Inside <script type='module'>: write function* range(n) yielding 1..n. Sum them with reduce inside a try block: const total = [...range(4)].reduce((a,b) => a+b, 0). console.log(total) โ€” should print 10. Also catch any error (none expected) and log 'no error'.

Recap: Iterators, fetch, errors (C4-C6)

50 XP5 min
Teoria

Quick recap โ€” Modules C4-C6

Async patterns and iteration. The 4 pieces that show up in every real codebase:

  1. `for...of` โ€” iterate any iterable: arrays, Maps, Sets, generators, NodeLists.
  2. **function* generator** โ€” yield values lazily. for-of consumes them.
  3. `try/catch` around `await` โ€” the rejection path. Without it, unhandled rejections bring down Node and noise up the browser.
  4. `Array.prototype.reduce` โ€” sum, group, build any aggregate in one pass.

If any feels random, redo C5-03 / C6-04 / C4-07 / C5-09 then come back.

๐Ÿ”’

Iscriviti per iniziare a programmare

La teoria รจ aperta a tutti. Editor, anteprima e check si sbloccano con una prova gratuita di 7 giorni โ€” carta richiesta, annullabile in qualsiasi momento.

Iscriviti โ€” prova gratuita โ†’

Le prime 10 lezioni di ogni percorso sono gratuite. Nessuna carta richiesta.

โ† PrecedenteProssima lezione โ†’

Ricevi un consiglio Python o web al giorno โ€” via email

Breve, scritto a mano, niente spam. Disiscriviti in un clic.