Skip to content

Commit 8613a17

Browse files
committed
translated until line 122
1 parent b5a2587 commit 8613a17

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

1-js/11-async/08-async-await/article.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# Async/await
22

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.
44

5-
## Async functions
5+
## Funcții Async
66

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:
88

99
```js
1010
async function f() {
1111
return 1;
1212
}
1313
```
1414

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ă.
1616

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:
1818

1919
```js run
2020
async function f() {
@@ -24,7 +24,7 @@ async function f() {
2424
f().then(alert); // 1
2525
```
2626

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:
2828

2929
```js run
3030
async function f() {
@@ -34,20 +34,20 @@ async function f() {
3434
f().then(alert); // 1
3535
```
3636

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.
3838

3939
## Await
4040

41-
The syntax:
41+
Sintaxa:
4242

4343
```js
44-
// works only inside async functions
44+
// funcționează numai în interiorul funcțiilor async
4545
let value = await promise;
4646
```
4747

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.
4949

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ă:
5151
```js run
5252
async function f() {
5353

@@ -56,23 +56,23 @@ async function f() {
5656
});
5757

5858
*!*
59-
let result = await promise; // wait until the promise resolves (*)
59+
let result = await promise; // așteptă până când promisiunea se rezolvă (*)
6060
*/!*
6161

62-
alert(result); // "done!"
62+
alert(result); // "gata!"
6363
}
6464

6565
f();
6666
```
6767

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ă.
6969

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.
7171

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.
7373

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ă:
7676

7777
```js run
7878
function f() {
@@ -83,32 +83,32 @@ function f() {
8383
}
8484
```
8585

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`.
8787
````
8888
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`:
9090
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.
9393
9494
```js run
9595
async function showAvatar() {
9696
97-
// read our JSON
97+
// citeșțe JSON-ul nostru
9898
let response = await fetch('/article/promise-chaining/user.json');
9999
let user = await response.json();
100100
101-
// read github user
101+
// citește utilizatorul github
102102
let githubResponse = await fetch(`https://api.github.com/users/${user.name}`);
103103
let githubUser = await githubResponse.json();
104104
105-
// show the avatar
105+
// arată avatarul
106106
let img = document.createElement('img');
107107
img.src = githubUser.avatar_url;
108108
img.className = "promise-avatar-example";
109109
document.body.append(img);
110110
111-
// wait 3 seconds
111+
// așteaptă 3 secunde
112112
await new Promise((resolve, reject) => setTimeout(resolve, 3000));
113113
114114
img.remove();
@@ -119,7 +119,7 @@ async function showAvatar() {
119119
showAvatar();
120120
```
121121
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.
123123
124124
````smart header="Modern browsers allow top-level `await` in modules"
125125
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

Comments
 (0)