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
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,21 +196,21 @@ new Waiter()
196
196
Semnificația este aceeași: se asigură că valoarea returnată este o promisiune și permite `await`.
197
197
198
198
````
199
-
## Error handling
199
+
## Gestionarea erorilor
200
200
201
-
If a promise resolves normally, then`await promise`returns the result. But in the case of a rejection, it throws the error, just as if there were a `throw`statement at that line.
201
+
Dacă o promisiune se rezolvă normal, atunci`await promise`returnează rezultatul. Dar în cazul unei respingeri, se aruncă eroarea, ca și cum ar exista o instrucțiune `throw`la linia respectivă.
202
202
203
-
This code:
203
+
Acest cod:
204
204
205
205
```js
206
206
asyncfunctionf() {
207
207
*!*
208
-
awaitPromise.reject(newError("Whoops!"));
208
+
awaitPromise.reject(newError("Uuups!"));
209
209
*/!*
210
210
}
211
211
```
212
212
213
-
...is the same as this:
213
+
...este la fel ca aceasta:
214
214
215
215
```js
216
216
asyncfunctionf() {
@@ -220,9 +220,9 @@ async function f() {
220
220
}
221
221
```
222
222
223
-
In real situations, the promise may take some time before it rejects. In that case there will be a delay before`await`throws an error.
223
+
În situații reale, promisiunea poate dura ceva timp înainte de a fi respinsă. În acest caz va exista o întârziere înainte ca`await`să arunce o eroare.
224
224
225
-
We can catch that error using `try..catch`, the same way as a regular `throw`:
225
+
Putem prinde această eroare folosind `try..catch`, la fel ca un `throw` obișnuit:
226
226
227
227
```js run
228
228
asyncfunctionf() {
@@ -239,7 +239,7 @@ async function f() {
239
239
f();
240
240
```
241
241
242
-
In the case of an error, the control jumps to the `catch` block. We can also wrap multiple lines:
242
+
În cazul unei erori, controlul sare la blocul `catch`. Putem de asemenea să înfășurăm mai multe linii:
243
243
244
244
```js run
245
245
asyncfunctionf() {
@@ -248,49 +248,49 @@ async function f() {
248
248
let response =awaitfetch('/no-user-here');
249
249
let user =awaitresponse.json();
250
250
} catch(err) {
251
-
//catches errors both in fetch and response.json
251
+
//prinde erorile atât din fetch cât și din response.json
252
252
alert(err);
253
253
}
254
254
}
255
255
256
256
f();
257
257
```
258
258
259
-
If we don't have `try..catch`, then the promise generated by the call of the async function `f()`becomes rejected. We can append `.catch`to handle it:
259
+
Dacă nu avem `try..catch`, atunci promisiunea generată de apelarea funcției asincrone `f()`devine respinsă. Putem adăuga `.catch`pentru a o gestiona:
260
260
261
261
```js run
262
262
asyncfunctionf() {
263
263
let response =awaitfetch('http://no-such-url');
264
264
}
265
265
266
-
// f() becomes a rejected promise
266
+
// f() devine o promisiune respinsă
267
267
*!*
268
268
f().catch(alert); // TypeError: failed to fetch // (*)
269
269
*/!*
270
270
```
271
271
272
-
If we forget to add `.catch`there, then we get an unhandled promise error (viewable in the console). We can catch such errors using a global `unhandledrejection` event handler as described in the chapter<info:promise-error-handling>.
272
+
Dacă uităm să adăugăm `.catch`acolo, atunci vom primi o eroare de promisiune negestionată (vizualizabilă în consolă). Putem prinde astfel de erori folosind un gestionar de evenimente global `unhandledrejection`, așa cum este descris în capitolul<info:promise-error-handling>.
When we use`async/await`, we rarely need `.then`, because`await`handles the waiting for us. And we can use a regular`try..catch`instead of `.catch`. That's usually (but not always) more convenient.
Atunci când folosim`async/await`, rareori avem nevoie de `.then`, deoarece`await`se ocupă de așteptare în locul nostru. Și putem folosi un`try..catch`obișnuit în loc de `.catch`. Acest lucru este de obicei (dar nu întotdeauna) mai convenabil.
277
277
278
-
But at the top level of the code, when we're outside any `async` function, we're syntactically unable to use `await`, so it's a normal practice to add `.then/catch`to handle the final result or falling-through error, like in the line `(*)`of the example above.
278
+
Dar la nivelul superior al codului, atunci când ne aflăm în afara oricărei funcții `async`, nu putem ca sintactic să folosim `await`, așa că este o practică normală să adăugăm `.then/catch`pentru a gestiona rezultatul final sau eroarea în cădere, ca în linia `(*)`din exemplul de mai sus.
279
279
```
280
280
281
-
````smart header="`async/await` works well with `Promise.all`"
282
-
When we need to wait for multiple promises, we can wrap them in `Promise.all` and then `await`:
281
+
````smart header="`async/await` funcționează bine cu `Promise.all`"
282
+
Atunci când trebuie să așteptăm mai multe promisiuni, le putem înfășura în `Promise.all` și apoi `await`:
283
283
284
284
```js
285
-
// wait for the array of results
285
+
// așteptă matricea de rezultate
286
286
let results = await Promise.all([
287
287
fetch(url1),
288
288
fetch(url2),
289
289
...
290
290
]);
291
291
```
292
292
293
-
In the case of an error, it propagates as usual, from the failed promise to`Promise.all`, and then becomes an exception that we can catch using `try..catch`around the call.
293
+
În cazul unei erori, aceasta se propagă ca de obicei, de la promisiunea eșuată la`Promise.all`, iar apoi devine o excepție pe care o putem prinde folosind `try..catch`în jurul apelului.
0 commit comments