File tree 7 files changed +103
-103
lines changed
1-js/11-async/08-async-await
7 files changed +103
-103
lines changed Original file line number Diff line number Diff line change 1
1
2
- The notes are below the code :
2
+ Notele se află sub cod :
3
3
4
4
``` js run
5
5
async function loadJson (url ) { // (1)
@@ -17,17 +17,17 @@ loadJson('https://javascript.info/no-such-user.json')
17
17
.catch (alert); // Error: 404 (4)
18
18
```
19
19
20
- Notes :
20
+ Note :
21
21
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 :
25
25
26
26
``` js
27
27
if (response .status == 200 ) {
28
28
return response .json (); // (3)
29
29
}
30
30
```
31
31
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 1
1
2
- # Rewrite using async/await
2
+ # Rescrieți folosind async/await
3
3
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 ` :
5
5
6
6
``` js run
7
7
function loadJson (url ) {
Original file line number Diff line number Diff line change 1
1
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 :
3
3
4
4
``` js run
5
5
class HttpError extends Error {
@@ -19,22 +19,22 @@ async function loadJson(url) {
19
19
}
20
20
}
21
21
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
23
23
async function demoGithubUser () {
24
24
25
25
let user;
26
26
while (true ) {
27
- let name = prompt (" Enter a name ?" , " iliakan" );
27
+ let name = prompt (" Introduceți un nume ?" , " iliakan" );
28
28
29
29
try {
30
30
user = await loadJson (` https://api.github.com/users/${ name} ` );
31
- break ; // no error, exit loop
31
+ break ; // nicio eroare, ieșiți din buclă
32
32
} catch (err) {
33
33
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 ." );
36
36
} else {
37
- // unknown error , rethrow
37
+ // eroare necunoscută , rethrow
38
38
throw err;
39
39
}
40
40
}
Original file line number Diff line number Diff line change 1
1
2
- # Rewrite "rethrow" with async/await
2
+ # Rescrieți "rethrow" cu async/await
3
3
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 ` .
5
5
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 .
7
7
8
8
``` js run
9
9
class HttpError extends Error {
@@ -25,18 +25,18 @@ function loadJson(url) {
25
25
});
26
26
}
27
27
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
29
29
function demoGithubUser () {
30
- let name = prompt (" Enter a name ?" , " iliakan" );
30
+ let name = prompt (" Introduceți un nume ?" , " iliakan" );
31
31
32
32
return loadJson (` https://api.github.com/users/${ name} ` )
33
33
.then (user => {
34
- alert (` Full name : ${ user .name } .` );
34
+ alert (` Nume complet : ${ user .name } .` );
35
35
return user;
36
36
})
37
37
.catch (err => {
38
38
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 ." );
40
40
return demoGithubUser ();
41
41
} else {
42
42
throw err;
Original file line number Diff line number Diff line change 1
1
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 .
3
3
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 ` :
5
5
``` js run
6
6
async function wait () {
7
7
await new Promise (resolve => setTimeout (resolve, 1000 ));
@@ -10,7 +10,7 @@ async function wait() {
10
10
}
11
11
12
12
function f () {
13
- // shows 10 after 1 second
13
+ // afișează 10 după 1 secundă
14
14
* ! *
15
15
wait ().then (result => alert (result));
16
16
*/ ! *
Original file line number Diff line number Diff line change 1
1
2
- # Call async from non-async
2
+ # Apelarea asincronă din non-asincronă
3
3
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 ` ?
5
5
6
6
``` js
7
7
async function wait () {
@@ -11,10 +11,10 @@ async function wait() {
11
11
}
12
12
13
13
function 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"
17
17
}
18
18
```
19
19
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