File tree Expand file tree Collapse file tree 7 files changed +103
-103
lines changed
1-js/11-async/08-async-await Expand file tree Collapse file tree 7 files changed +103
-103
lines changed Original file line number Diff line number Diff line change 11
2- The notes are below the code :
2+ Notele se află sub cod :
33
44``` js run
55async function loadJson (url ) { // (1)
@@ -17,17 +17,17 @@ loadJson('https://javascript.info/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 . Funcția ` loadJson ` devine ` async ` .
23+ 2 . Toate funcțiile ` .then ` din interior sunt înlocuite cu ` await ` .
24+ 3 . Putem ` returna response.json()` în loc să-l așteptăm, astfel :
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+ Apoi codul exterior va trebui să ` await` ca promisiunea să se rezolve. În cazul nostru, nu contează .
33+ 4. Eroarea aruncată de ` loadJson` este gestionată de ` .catch` . Nu putem folosi ` await loadJson(…)` acolo, pentru că nu suntem într - o funcție ` async` .
Original file line number Diff line number Diff line change 11
2- # Rewrite using async/await
2+ # Rescrieți folosind async/await
33
4- Rewrite this example code from the chapter < info:promise-chaining > using ` async/await ` instead of ` .then/catch ` :
4+ Rescrieți acest exemplu de cod din capitolul < info:promise-chaining > folosind ` async/await ` în loc de ` .then/catch ` :
55
66``` js run
77function loadJson (url ) {
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+ Nu există trucuri aici. Doar înlocuiți ` .catch ` cu ` try..catch ` în ` demoGithubUser ` și adăugați ` async/await ` acolo unde este necesar :
33
44``` js run
55class HttpError extends Error {
@@ -19,22 +19,22 @@ async function loadJson(url) {
1919 }
2020}
2121
22- // Ask for a user name until github returns a valid user
22+ // Cere un nume de utilizator până când github returnează un utilizator valid
2323async function demoGithubUser () {
2424
2525 let user;
2626 while (true ) {
27- let name = prompt (" Enter a name ?" , " iliakan" );
27+ let name = prompt (" Introduceți un nume ?" , " iliakan" );
2828
2929 try {
3030 user = await loadJson (` https://api.github.com/users/${ name} ` );
31- break ; // no error, exit loop
31+ break ; // nicio eroare, ieșiți din buclă
3232 } catch (err) {
3333 if (err instanceof HttpError && err .response .status == 404 ) {
34- // loop continues after the alert
35- alert (" No such user, please reenter ." );
34+ // bucla continuă după alertă
35+ alert (" Nu există un astfel de utilizator, vă rugăm să îl introduceți din nou ." );
3636 } else {
37- // unknown error , rethrow
37+ // eroare necunoscută , rethrow
3838 throw err;
3939 }
4040 }
Original file line number Diff line number Diff line change 11
2- # Rewrite "rethrow" with async/await
2+ # Rescrieți "rethrow" cu async/await
33
4- Below you can find the "rethrow" example. Rewrite it using ` async/await ` instead of ` .then/catch ` .
4+ Mai jos puteți găsi exemplul "rethrow". Rescrieți-l folosind ` async/await ` în loc de ` .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+ Și scăpați de recursivitate în favoarea unei bucle în ` demoGithubUser ` : cu ` async/await ` acest lucru devine ușor de făcut .
77
88``` js run
99class HttpError extends Error {
@@ -25,18 +25,18 @@ function loadJson(url) {
2525 });
2626}
2727
28- // Ask for a user name until github returns a valid user
28+ // Cere un nume de utilizator până când github returnează un utilizator valid
2929function demoGithubUser () {
30- let name = prompt (" Enter a name ?" , " iliakan" );
30+ let name = prompt (" Introduceți un nume ?" , " iliakan" );
3131
3232 return loadJson (` https://api.github.com/users/${ name} ` )
3333 .then (user => {
34- alert (` Full name : ${ user .name } .` );
34+ alert (` Nume complet : ${ user .name } .` );
3535 return user;
3636 })
3737 .catch (err => {
3838 if (err instanceof HttpError && err .response .status == 404 ) {
39- alert (" No such user, please reenter ." );
39+ alert (" Nu există un astfel de utilizator, vă rugăm să îl introduceți din nou ." );
4040 return demoGithubUser ();
4141 } else {
4242 throw err;
Original file line number Diff line number Diff line change 11
2- That's the case when knowing how it works inside is helpful .
2+ Acesta este cazul în care este util să știți cum funcționează în interior .
33
4- Just treat ` async ` call as promise and attach ` .then ` to it :
4+ Pur și simplu tratați apelul ` async ` ca pe o promisiune și atașați-i ` .then ` :
55``` js run
66async function wait () {
77 await new Promise (resolve => setTimeout (resolve, 1000 ));
@@ -10,7 +10,7 @@ async function wait() {
1010}
1111
1212function f () {
13- // shows 10 after 1 second
13+ // afișează 10 după 1 secundă
1414* ! *
1515 wait ().then (result => alert (result));
1616*/ ! *
Original file line number Diff line number Diff line change 11
2- # Call async from non-async
2+ # Apelarea asincronă din non-asincronă
33
4- We have a "regular" function called ` f ` . How can you call the ` async ` function ` wait() ` and use its result inside of ` f ` ?
4+ Avem o funcție "obișnuită" numită ` f ` . Cum puteți apela funcția ` async ` ` wait() ` și să utilizați rezultatul acesteia în interiorul lui ` f ` ?
55
66``` js
77async function wait () {
@@ -11,10 +11,10 @@ async function wait() {
1111}
1212
1313function f () {
14- // ...what should you write here ?
15- // we need to call async wait() and wait to get 10
16- // remember, we can't use "await"
14+ // ...ce ar trebui să scrieți aici ?
15+ // trebuie să apelăm async wait() și să așteptăm să primim 10
16+ // rețineți, nu putem folosi "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. Sarcina este foarte simplă din punct de vedere tehnic, dar întrebarea este destul de frecventă pentru dezvoltatorii noi în async/await.
You can’t perform that action at this time.
0 commit comments