File tree Expand file tree Collapse file tree 5 files changed +80
-79
lines changed
1-js/11-async/08-async-await Expand file tree Collapse file tree 5 files changed +80
-79
lines changed Original file line number Diff line number Diff line change 11
2- The notes are below the code :
2+ Le note sono sotto il codice :
33
44``` js run
55async function loadJson (url ) { // (1)
@@ -17,17 +17,17 @@ loadJson('no-such-user.json')
1717 .catch (alert); // Error: 404 (4)
1818```
1919
20- Notes :
20+ Note :
2121
22- 1 . The function ` loadJson ` becomes ` async ` .
23- 2 . All ` .then ` inside are replaced with ` await ` .
24- 3 . We can ` return response.json()` instead of awaiting for it, like this :
22+ 1 . La funzione ` loadJson ` diventa ` async ` .
23+ 2 . Tutti i ` .then ` interni sono sostituiti con ` await ` .
24+ 3 . Possiamo ritornare ` response.json() ` invece di aspettarlo ( awaiting for it), come qui :
2525
2626 ``` js
2727 if (response .status == 200 ) {
2828 return response .json (); // (3)
2929 }
3030 ```
3131
32- Then the outer code would have to ` await` for that promise to resolve . In our case it doesn ' t matter .
33- 4. The error thrown from `loadJson` is handled by `.catch`. We can ' t use ` await loadJson(…)` there, because we ' re not in an `async` function .
32+ Poi il codice esterno avrebbe dovuto attendere ( ` await) ` che la promise risolvesse . Nel nostro caso non è importante .
33+ 4. L ' errore sollevato da `loadJson` è gestito da `.catch`. Non possiamo usare `await loadJson(…)` qui, perchè non siamo in una funzione `async`.
Original file line number Diff line number Diff line change 11
2- There are no tricks here. Just replace ` .catch ` with ` try...catch ` inside ` demoGithubUser ` and add ` async/await ` where needed :
2+ Non ci sono trucchi qui. Basta sostituire ` .catch ` with ` try...catch ` dentro ` demoGithubUser ` e aggiungere ` async/await ` quando necessario :
33
44``` js run
55class HttpError extends Error {
Original file line number Diff line number Diff line change 11
2- # Rewrite "rethrow" with async/await
2+ # Riscrivere "rethrow" con async/await
33
4- Below you can find the "rethrow" example from the chapter < info:promise-chaining > . Rewrite it using ` async/await ` instead of ` .then/catch ` .
4+ Sotto puoi trovare l'esempio "rethrow" dal capitolo < info:promise-chaining > . Riscriverlo usando ` async/await ` invece di ` .then/catch ` .
55
6- And get rid of the recursion in favour of a loop in ` demoGithubUser ` : with ` async/await ` that becomes easy to do .
6+ Sbarazzarsi della ricorsione a favore di un ciclo in ` demoGithubUser ` : con ` async/await ` diventa facile da fare .
77
88``` js run
99class HttpError extends Error {
Original file line number Diff line number Diff line change 11
22# Call async from non-async
33
4- We have a "regular" function. How to call ` async ` from it and use its result ?
4+ Abbiamo una funzione "regolare". Come chiamare ` async ` da questa ed usare il suo risultato ?
55
66``` js
77async function wait () {
@@ -11,10 +11,10 @@ async function wait() {
1111}
1212
1313function f () {
14- // ...what to write here ?
15- // we need to call async wait() and wait to get 10
16- // remember, we can't use "await"
14+ // ...cosa bisonga scrivere qui ?
15+ // dobbiamo chiamare async wait() ed aspettare per ricevere 10
16+ // ricorda, non possiamo usare "await"
1717}
1818```
1919
20- P.S. The task is technically very simple, but the question is quite common for developers new to async/await.
20+ P.S. Il task è tecnicamente molto semplice, ma la domanda è piuttosto comune per gli sviluppatori nuovi ad async/await.
You can’t perform that action at this time.
0 commit comments