Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer 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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 2

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Awesome!

Completion rate improved to 3.57

bookUsing Await for Asynchronous Flow

Veeg om het menu te tonen

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

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 2
some-alt