Question: 1 / 155

What is the correct code to replace the last line with an async/await function for retrieving a book?

async function getBookInfo() { const Id = await getId; const result = await getBook(id); }

The correct code is structured to take full advantage of the async/await syntax, which simplifies handling asynchronous operations in JavaScript. In this case, the purpose is to retrieve a book using an identifier obtained from another asynchronous function.

In the chosen option, the function is declared with the async keyword, which allows the use of the await keyword inside it. This is important because it indicates that the function contains asynchronous operations that will pause execution until the promise is resolved. The first line of the function retrieves an identifier using an asynchronous operation marked with await. Here, the await expression pauses the function execution until the promise returned by getId is resolved and assigns the resolved value to the variable named `Id`.

The next line utilizes that identifier to fetch book information by invoking another asynchronous function, getBook, also using the await keyword. This approach ensures that the function waits for the operation to complete before proceeding, allowing for cleaner and more manageable asynchronous code, particularly when dealing with multiple asynchronous calls.

This structure is effective as it clearly demonstrates how to work with promises in a readable format, coordinating multiple asynchronous requests without deeply nested callback structures, which can lead to what is often referred to as "callback hell."

The other options contain various issues, such as not using

async function getBookInfo() { const Id = await getId; const result = await getBook(Id); }

await function getBookInfo() { const Id = getId; const result = getBook(Id); }

function getBookInfo() { const Id = getId; const result = await getBook(Id); }

Next

Report this question