You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/11-async/08-async-await/article.md
+27-27Lines changed: 27 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,20 @@
1
1
# Async/await
2
2
3
-
There's a special syntax to work with promises in a more comfortable fashion, called "async/await". It's surprisingly easy to understand and use.
3
+
Există o sintaxă specială pentru a lucra cu promisiuni într-un mod mai confortabil, numită "async/await". Este surprinzător de ușor de înțeles și de utilizat.
4
4
5
-
## Async functions
5
+
## Funcții Async
6
6
7
-
Let's start with the `async` keyword. It can be placed before a function, like this:
7
+
Să începem de la cuvântul cheie `async`. Acesta poate fi plasat înaintea unei funcții, astfel:
8
8
9
9
```js
10
10
asyncfunctionf() {
11
11
return1;
12
12
}
13
13
```
14
14
15
-
The word "async" before a function means one simple thing: a function always returns a promise. Other values are wrapped in a resolved promise automatically.
15
+
Cuvântul "async" înaintea unei funcții înseamnă un lucru simplu: o funcție returnează întotdeauna o promisiune. Alte valori sunt incluse automat într-o promisiune rezolvată.
16
16
17
-
For instance, this function returns a resolved promise with the result of `1`; let's test it:
17
+
De exemplu, această funcție returnează o promisiune rezolvată cu rezultatul `1`; haideți să o testăm:
18
18
19
19
```js run
20
20
asyncfunctionf() {
@@ -24,7 +24,7 @@ async function f() {
24
24
f().then(alert); // 1
25
25
```
26
26
27
-
...We could explicitly return a promise, which would be the same:
27
+
...Am putea returna în mod explicit o promisiune, ceea ce ar fi la fel:
28
28
29
29
```js run
30
30
asyncfunctionf() {
@@ -34,20 +34,20 @@ async function f() {
34
34
f().then(alert); // 1
35
35
```
36
36
37
-
So, `async`ensures that the function returns a promise, and wraps non-promises in it. Simple enough, right? But not only that. There's another keyword, `await`, that works only inside `async` functions, and it's pretty cool.
37
+
Deci, `async`se asigură că funcția returnează o promisiune, și înfășoară în ea non-promisiuni. Destul de simplu, nu? Dar nu numai atât. Mai există un alt cuvânt cheie, `await`, care funcționează numai în interiorul funcțiilor `async`, și este destul de cool.
38
38
39
39
## Await
40
40
41
-
The syntax:
41
+
Sintaxa:
42
42
43
43
```js
44
-
//works only inside async functions
44
+
//funcționează numai în interiorul funcțiilor async
45
45
let value =await promise;
46
46
```
47
47
48
-
The keyword`await`makes JavaScript wait until that promise settles and returns its result.
48
+
Cuvântul cheie`await`face ca JavaScript să aștepte până când promisiunea respectivă se soluționează și îi returnează rezultatul.
49
49
50
-
Here's an example with a promise that resolves in 1 second:
50
+
Iată un exemplu cu o promisiune care se rezolvă în 1 secundă:
51
51
```js run
52
52
asyncfunctionf() {
53
53
@@ -56,23 +56,23 @@ async function f() {
56
56
});
57
57
58
58
*!*
59
-
let result =await promise; //wait until the promise resolves (*)
59
+
let result =await promise; //așteptă până când promisiunea se rezolvă (*)
60
60
*/!*
61
61
62
-
alert(result); // "done!"
62
+
alert(result); // "gata!"
63
63
}
64
64
65
65
f();
66
66
```
67
67
68
-
The function execution "pauses" at the line `(*)`and resumes when the promise settles, with `result`becoming its result. So the code above shows "done!" in one second.
68
+
Executarea funcției "se oprește" la linia `(*)`și se reia când promisiunea se soluționează, `result`devenind rezultatul acesteia. Astfel, codul de mai sus arată "gata!" într-o secundă.
69
69
70
-
Let's emphasize: `await`literally suspends the function execution until the promise settles, and then resumes it with the promise result. That doesn't cost any CPU resources, because the JavaScript engine can do other jobs in the meantime: execute other scripts, handle events, etc.
70
+
Să subliniem: `await`suspendă literalmente execuția funcției până când promisiunea se soluționează, iar apoi o reia cu rezultatul promisiunii. Acest lucru nu costă nicio resursă de CPU, deoarece motorul JavaScript poate face între timp și alte sarcini: să execute alte scripturi, să gestioneze evenimente etc.
71
71
72
-
It's just a more elegant syntax of getting the promise result than`promise.then`. And, it's easier to read and write.
72
+
Este doar o sintaxă mai elegantă de a obține rezultatul promisiunii decât`promise.then`. Și, este mai ușor de citit și de scris.
73
73
74
-
````warn header="Can't use `await`in regular functions"
75
-
If we try to use`await`in a non-async function, there would be a syntax error:
74
+
````warn header="Nu se poate folosi `await`în funcții obișnuite"
75
+
Dacă am încerca să folosim`await`într-o funcție non-async, ar apărea o eroare de sintaxă:
76
76
77
77
```js run
78
78
functionf() {
@@ -83,32 +83,32 @@ function f() {
83
83
}
84
84
```
85
85
86
-
We may get this error if we forget to put `async`before a function. As stated earlier, `await`only works inside an `async` function.
86
+
Putem primi această eroare dacă uităm să punem `async`înaintea unei funcții. După cum am menționat mai devreme, `await`funcționează numai în interiorul unei funcții `async`.
87
87
````
88
88
89
-
Let's take the `showAvatar()` example from the chapter <info:promise-chaining> and rewrite it using `async/await`:
89
+
Să luăm exemplul `showAvatar()` din capitolul <info:promise-chaining> și să îl rescriem folosind `async/await`:
90
90
91
-
1. We'll need to replace `.then` calls with `await`.
92
-
2. Also we should make the function `async` for them to work.
91
+
1. Va trebui să înlocuim apelurile `.then` cu `await`.
92
+
2. De asemenea ar trebui să facem funcția `async` pentru ca acestea să funcționeze.
93
93
94
94
```js run
95
95
async function showAvatar() {
96
96
97
-
// read our JSON
97
+
// citeșțe JSON-ul nostru
98
98
let response = await fetch('/article/promise-chaining/user.json');
99
99
let user = await response.json();
100
100
101
-
// read github user
101
+
// citește utilizatorul github
102
102
let githubResponse = await fetch(`https://api.github.com/users/${user.name}`);
103
103
let githubUser = await githubResponse.json();
104
104
105
-
// show the avatar
105
+
// arată avatarul
106
106
let img = document.createElement('img');
107
107
img.src = githubUser.avatar_url;
108
108
img.className = "promise-avatar-example";
109
109
document.body.append(img);
110
110
111
-
// wait 3 seconds
111
+
// așteaptă 3 secunde
112
112
await new Promise((resolve, reject) => setTimeout(resolve, 3000));
113
113
114
114
img.remove();
@@ -119,7 +119,7 @@ async function showAvatar() {
119
119
showAvatar();
120
120
```
121
121
122
-
Pretty clean and easy to read, right? Much better than before.
122
+
Destul de curat și ușor de citit, nu? Mult mai bine decât înainte.
123
123
124
124
````smart header="Modern browsers allow top-level `await` in modules"
125
125
In modern browsers, `await` on top level works just fine, when we're inside a module. We'll cover modules in article <info:modules-intro>.
0 commit comments