File tree 7 files changed +97
-97
lines changed
1-js/11-async/08-async-await
7 files changed +97
-97
lines changed Original file line number Diff line number Diff line change 1
1
2
- The notes are below the code :
2
+ Las notas están bajo el código :
3
3
4
4
``` js run
5
5
async function loadJson (url ) { // (1)
@@ -17,17 +17,17 @@ loadJson('no-such-user.json')
17
17
.catch (alert); // Error: 404 (4)
18
18
```
19
19
20
- Notes :
20
+ Notas :
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 . La función ` loadJson ` se vuelve ` async ` .
23
+ 2 . Todo lo que está dentro de ` .then ` es reemplazado por ` await ` .
24
+ 3 . Podemos devolver ` return response.json() ` en lugar de esperar por él, como esto :
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
+ Entonces el código externo tendría que esperar que la promesa se resuelva . En nuestro caso eso no importa .
33
+ 4. El error arrojado por ` loadJson` es manejado por ` .catch` . No podemos usar ` await loadJson(…)` allí, porque no estamos en una función ` async` .
Original file line number Diff line number Diff line change 1
1
2
- # Rewrite using async/await
2
+ # Rescribir usando async/await
3
3
4
- Rewrite this example code from the chapter < info:promise-chaining > using ` async/await ` instead of ` .then/catch ` :
4
+ Rescribir este código de ejemplo del capítulo < info:promise-chaining > usando ` async/await ` en vez 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
+ No hay trampas aquí. Simplemente reemplaza ` .catch ` con ` try...catch ` dentro de ` demoGithubUser ` y agregar ` async/await ` donde sea necesario :
3
3
4
4
``` js run
5
5
class HttpError extends Error {
@@ -24,15 +24,15 @@ 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 (" Ingrese un nombre: " , " iliakan" );
28
28
29
29
try {
30
30
user = await loadJson (` https://api.github.com/users/${ name} ` );
31
31
break ; // no error, exit loop
32
32
} catch (err) {
33
33
if (err instanceof HttpError && err .response .status == 404 ) {
34
34
// loop continues after the alert
35
- alert (" No such user, please reenter ." );
35
+ alert (" No existe tal usuario, por favor reingrese ." );
36
36
} else {
37
37
// unknown error, rethrow
38
38
throw err;
@@ -41,7 +41,7 @@ async function demoGithubUser() {
41
41
}
42
42
43
43
44
- alert (` Full name : ${ user .name } .` );
44
+ alert (` Nombre completo : ${ user .name } .` );
45
45
return user;
46
46
}
47
47
Original file line number Diff line number Diff line change 1
1
2
- # Rewrite "rethrow" with async/await
2
+ # Reescribir "rethrow" con async/await
3
3
4
- Below you can find the "rethrow" example from the chapter < info:promise-chaining > . Rewrite it using ` async/await ` instead of ` .then/catch ` .
4
+ Debajo puedes encontrar el ejemplo "rethrow" del capítulo < info:promise-chaining > . Rescríbelo usando ` async/await ` en vez 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
+ Y deshazte de la recursión en favor de un bucle en ` demoGithubUser ` : con ` async/await ` , que se vuelve fácil de hacer .
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
+ // Pide nombres hasta que github devuelve un usuario válido
29
29
function demoGithubUser () {
30
- let name = prompt (" Enter a name? " , " iliakan" );
30
+ let name = prompt (" Ingrese un nombre: " , " 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 (` Nombre completo : ${ 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 (" No existe tal usuario, por favor reingrese ." );
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
+ Este es el caso cuando saber cómo trabaja por dentro es útil .
3
3
4
- Just treat ` async ` call as promise and attach ` .then ` to it :
4
+ Simplemente trata el llamado ` async ` como una promesa y añádele ` .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
+ // muestra 10 después de 1 segundo
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
+ # Llamado async desde un non-async
3
3
4
- We have a "regular" function. How to call ` async ` from it and use its result ?
4
+ Tenemos una función "regular". ¿Cómo llamar ` async ` desde ella y usar el resultado ?
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 to write here ?
15
- // we need to call async wait() and wait to get 10
16
- // remember, we can't use "await"
14
+ // ¿ ...qué escribir aquí ?
15
+ // Necesitamos llamar async wait() y esperar a obtener 10
16
+ // recuerda, no podemos usar "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.D. La tarea es técnicamente muy simple, pero la pregunta es muy común en desarrolladores nuevos en async/await.
You can’t perform that action at this time.
0 commit comments