File tree Expand file tree Collapse file tree 7 files changed +97
-97
lines changed
1-js/11-async/08-async-await Expand file tree Collapse file tree 7 files changed +97
-97
lines changed Original file line number Diff line number Diff line change 11
2- The notes are below the code :
2+ Las notas están bajo el código :
33
44``` js run
55async function loadJson (url ) { // (1)
@@ -17,17 +17,17 @@ loadJson('no-such-user.json')
1717 .catch (alert); // Error: 404 (4)
1818```
1919
20- Notes :
20+ Notas :
2121
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 :
2525
2626 ``` js
2727 if (response .status == 200 ) {
2828 return response .json (); // (3)
2929 }
3030 ```
3131
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 11
2- # Rewrite using async/await
2+ # Rescribir usando async/await
33
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 ` :
55
66``` js run
77function loadJson (url ) {
Original file line number Diff line number Diff line change 11
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 :
33
44``` js run
55class HttpError extends Error {
@@ -24,15 +24,15 @@ async function demoGithubUser() {
2424
2525 let user;
2626 while (true ) {
27- let name = prompt (" Enter a name? " , " iliakan" );
27+ let name = prompt (" Ingrese un nombre: " , " iliakan" );
2828
2929 try {
3030 user = await loadJson (` https://api.github.com/users/${ name} ` );
3131 break ; // no error, exit loop
3232 } catch (err) {
3333 if (err instanceof HttpError && err .response .status == 404 ) {
3434 // loop continues after the alert
35- alert (" No such user, please reenter ." );
35+ alert (" No existe tal usuario, por favor reingrese ." );
3636 } else {
3737 // unknown error, rethrow
3838 throw err;
@@ -41,7 +41,7 @@ async function demoGithubUser() {
4141 }
4242
4343
44- alert (` Full name : ${ user .name } .` );
44+ alert (` Nombre completo : ${ user .name } .` );
4545 return user;
4646}
4747
Original file line number Diff line number Diff line change 11
2- # Rewrite "rethrow" with async/await
2+ # Reescribir "rethrow" con async/await
33
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 ` .
55
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 .
77
88``` js run
99class HttpError extends Error {
@@ -25,18 +25,18 @@ function loadJson(url) {
2525 })
2626}
2727
28- // Ask for a user name until github returns a valid user
28+ // Pide nombres hasta que github devuelve un usuario válido
2929function demoGithubUser () {
30- let name = prompt (" Enter a name? " , " iliakan" );
30+ let name = prompt (" Ingrese un nombre: " , " iliakan" );
3131
3232 return loadJson (` https://api.github.com/users/${ name} ` )
3333 .then (user => {
34- alert (` Full name : ${ user .name } .` );
34+ alert (` Nombre completo : ${ user .name } .` );
3535 return user;
3636 })
3737 .catch (err => {
3838 if (err instanceof HttpError && err .response .status == 404 ) {
39- alert (" No such user, please reenter ." );
39+ alert (" No existe tal usuario, por favor reingrese ." );
4040 return demoGithubUser ();
4141 } else {
4242 throw err;
Original file line number Diff line number Diff line change 11
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 .
33
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 ` :
55``` js run
66async function wait () {
77 await new Promise (resolve => setTimeout (resolve, 1000 ));
@@ -10,7 +10,7 @@ async function wait() {
1010}
1111
1212function f () {
13- // shows 10 after 1 second
13+ // muestra 10 después de 1 segundo
1414* ! *
1515 wait ().then (result => alert (result));
1616*/ ! *
Original file line number Diff line number Diff line change 11
2- # Call async from non-async
2+ # Llamado async desde un non-async
33
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 ?
55
66``` js
77async function wait () {
@@ -11,10 +11,10 @@ async function wait() {
1111}
1212
1313function 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"
1717}
1818```
1919
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