Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Using Await for Asynchronous Flow | Async/Await
Asynchronous JavaScript Explained

bookUsing Await for Asynchronous Flow

script.js

script.js

index.html

index.html

copy

This code demonstrates how the await keyword can be used inside an async function to pause execution until a Promise resolves. You start by defining an async function named fetchData. Inside this function, you create a new Promise called dataPromise, which simulates an asynchronous data fetch using setTimeout. The promise resolves with the string "Data loaded!" after one second.

The crucial line is const result = await dataPromise;. Here, the await keyword tells JavaScript to pause execution of the fetchData function until dataPromise is resolved. Once the promise resolves, the resolved value ("Data loaded!") is assigned to the result variable, and the next line (console.log(result);) is executed, printing the result to the console.

Using await in this way makes asynchronous code easier to read and write. Without await, you would need to use .then() chaining, which can quickly become hard to follow as your logic grows. With await, your asynchronous code appears more like synchronous code, improving clarity and maintainability.

question mark

What does the await keyword do in an async function?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Awesome!

Completion rate improved to 3.57

bookUsing Await for Asynchronous Flow

Desliza para mostrar el menú

script.js

script.js

index.html

index.html

copy

This code demonstrates how the await keyword can be used inside an async function to pause execution until a Promise resolves. You start by defining an async function named fetchData. Inside this function, you create a new Promise called dataPromise, which simulates an asynchronous data fetch using setTimeout. The promise resolves with the string "Data loaded!" after one second.

The crucial line is const result = await dataPromise;. Here, the await keyword tells JavaScript to pause execution of the fetchData function until dataPromise is resolved. Once the promise resolves, the resolved value ("Data loaded!") is assigned to the result variable, and the next line (console.log(result);) is executed, printing the result to the console.

Using await in this way makes asynchronous code easier to read and write. Without await, you would need to use .then() chaining, which can quickly become hard to follow as your logic grows. With await, your asynchronous code appears more like synchronous code, improving clarity and maintainability.

question mark

What does the await keyword do in an async function?

Select the correct answer

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 2
some-alt