Skip to content

Commit 458bced

Browse files
author
mean2me
committed
Fix formatting issue
1 parent 8d75e18 commit 458bced

File tree

1 file changed

+32
-26
lines changed
  • 1-js/12-generators-iterators/2-async-iterators-generators

1 file changed

+32
-26
lines changed

1-js/12-generators-iterators/2-async-iterators-generators/article.md

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Ecco una semplice tabella di riepilogo:
119119
| Il valore ritornato da `next()` è | qualsiasi valore | `Promise` |
120120
| ciclo da utilizzare | `for..of` | `for await..of` |
121121

122-
````warn header="Lo spread operator `...` non funziona in modo asincrono"
122+
````warn header="Lo spread operator '...' non funziona in modo asincrono"
123123
Le funzionalità offerte dai comuni iteratori (sincroni) non sono disponibili per gli iteratori asincroni.
124124
125125
Per esempio, lo spread operator non puà essere utilizzato:
@@ -136,7 +136,7 @@ Questo è prevedibile, dal momento che lo spread operator ha bisogno di `S
136136

137137
Come già sappiamo, JavaScript supporta anche i cosiddetti generatori, che sono anche iteratori.
138138

139-
Ricordiamo l'esempio del generatore di una sequenza di numeri da `start` a `end`, nel capitolo [](info:generators):
139+
Ricordiamo l'esempio del generatore di una sequenza di numeri da `start` a `end`, nel capitolo [generatori](info:generators):
140140

141141
```js run
142142
function* generateSequence(start, end) {
@@ -145,7 +145,7 @@ function* generateSequence(start, end) {
145145
}
146146
}
147147

148-
for(let value of generateSequence(1, 5)) {
148+
for (let value of generateSequence(1, 5)) {
149149
alert(value); // 1, poi 2, poi 3, poi 4, poi 5
150150
}
151151
```
@@ -218,14 +218,15 @@ let range = {
218218
from: 1,
219219
to: 5,
220220
221-
*[Symbol.iterator]() { // sintassi compatta di [Symbol.iterator]: function*()
222-
for(let value = this.from; value <= this.to; value++) {
221+
*[Symbol.iterator]() {
222+
// sintassi compatta di [Symbol.iterator]: function*()
223+
for (let value = this.from; value <= this.to; value++) {
223224
yield value;
224225
}
225-
}
226+
},
226227
};
227228
228-
for(let value of range) {
229+
for (let value of range) {
229230
alert(value); // 1, poi 2, poi 3, poi 4, poi 5
230231
}
231232
```
@@ -278,7 +279,7 @@ E' un modello molto comune, non solo per gli utenti, ma per qualsiasi cosa. Ad e
278279
Ci piacerebbe, tuttavia, avere una API pi&ugrave; semplice: un oggetto iteratore per le commit, che ci consenta di elencarle nel seguente modo:
279280

280281
```js
281-
let repo = 'javascript-tutorial/en.javascript.info'; // repository GitHub dal quale ottenere le commit
282+
let repo = "javascript-tutorial/en.javascript.info"; // repository GitHub dal quale ottenere le commit
282283
283284
for await (let commit of fetchCommits(repo)) {
284285
// process commit
@@ -294,19 +295,21 @@ async function* fetchCommits(repo) {
294295
let url = `https://api.github.com/repos/${repo}/commits`;
295296
296297
while (url) {
297-
const response = await fetch(url, { // (1)
298-
headers: {'User-Agent': 'Our script'}, // github richiede un header user-agent
298+
const response = await fetch(url, {
299+
// (1)
300+
headers: { "User-Agent": "Our script" }, // github richiede un header user-agent
299301
});
300302
301303
const body = await response.json(); // (2) la risposta &egrave; un JSON (array di commit)
302304
303305
// (3) la URL della pagina successiva &egrave; negli header, dunque dobbiamo estrarla
304-
let nextPage = response.headers.get('Link').match(/<(.*?)>; rel="next"/);
306+
let nextPage = response.headers.get("Link").match(/<(.*?)>; rel="next"/);
305307
nextPage = nextPage && nextPage[1];
306308
307309
url = nextPage;
308310
309-
for(let commit of body) { // (4) restituisce (yield) le commit una ad una fino alla fine della pagina
311+
for (let commit of body) {
312+
// (4) restituisce (yield) le commit una ad una fino alla fine della pagina
310313
yield commit;
311314
}
312315
}
@@ -322,18 +325,18 @@ Un esempio di utilizzo (visualizza gli autori delle commit nella console):
322325
323326
```js run
324327
(async () => {
325-
326328
let count = 0;
327329
328-
for await (const commit of fetchCommits('javascript-tutorial/en.javascript.info')) {
329-
330+
for await (const commit of fetchCommits(
331+
"javascript-tutorial/en.javascript.info"
332+
)) {
330333
console.log(commit.author.login);
331334
332-
if (++count == 100) { // let's stop at 100 commits
335+
if (++count == 100) {
336+
// let's stop at 100 commits
333337
break;
334338
}
335339
}
336-
337340
})();
338341
```
339342

@@ -347,21 +350,24 @@ Quando i dati ci arrivano in modo asincrono, con dei ritardi, possiamo usare ite
347350

348351
Differenze sintattiche tra iteratori sincroni e asincroni:
349352

350-
| | Iteratori | Iteratori asincroni |
351-
|-------------------------------------|-------------------|------------------------|
352-
| Metodo che ci fornisce l'iteratore | `Symbol.iterator` | `Symbol.asyncIterator` |
353-
| valore ritornato da `next()` | qualsiasi valore | `Promise` |
353+
| | Iteratori | Iteratori asincroni |
354+
| ---------------------------------- | ----------------- | ---------------------- |
355+
| Metodo che ci fornisce l'iteratore | `Symbol.iterator` | `Symbol.asyncIterator` |
356+
| valore ritornato da `next()` | qualsiasi valore | `Promise` |
354357

355358
Differenze sintattiche tra generatori asincroni e sincroni:
356359

357-
| | Generators | Async generators |
358-
|--------------------------------|-------------------------------|----------------------------------------------------------------|
359-
| Dichiarazione | `function*` | `async function*` |
360-
| `generator.next()` ritorna... | `{value:…, done: true/false}` | `Promise` che risolve ritornando `{value:…, done: true/false}` |
360+
| | Generators | Async generators |
361+
| ----------------------------- | ----------------------------- | -------------------------------------------------------------- |
362+
| Dichiarazione | `function*` | `async function*` |
363+
| `generator.next()` ritorna... | `{value:…, done: true/false}` | `Promise` che risolve ritornando `{value:…, done: true/false}` |
361364

362365
Nello sviluppo web incontriamo spesso flussi di dati che vengono ritornati "in gruppi". Per esempio, il download o l'upload di file grandi.
363366

364367
Possiamo usare i generatori asincroni per processare questo tipo di dati but vale anche la pena di menzionare che c'&egrave; un'altra API, chiamata Streams, che ci fornisce delle interfacce speciali per gestire questi flussi di dati, per trasformarli e passarli ad altri flussi per ulteriori manipolazioni (ad esempio scaricare dati da qualche sorgente e poi immediatamente inviarli da qualche parte).
365368

366369
Le Streams API non fanno parte del linguaggio JavaScript standard.
367-
````
370+
371+
```
372+
373+
```

0 commit comments

Comments
 (0)