Complete the code to declare an asynchronous function.
async function fetchData() {
const data = await fetch([1]);
return data.json();
}fetch inside the blank instead of just the URL string.await inside the argument of fetch.The fetch function takes a URL string as argument. Here, the URL string must be passed inside the fetch() call. The await keyword waits for the fetch promise to resolve.
Complete the code to wait for the promise result inside an async function.
async function getUser() {
const user = [1] fetchUserFromDB();
return user;
}then instead of await inside async functions.await and expecting the resolved value.The await keyword pauses the async function until the promise resolves, then returns the result.
Fix the error in the async function declaration.
function [1] fetchData() { const response = await fetch('https://api.example.com'); return response.json(); }
await before the function keyword.function* instead of async.The async keyword must be placed before the function keyword to declare an async function. This allows use of await inside.
Fill both blanks to correctly handle errors with try/catch in async function.
async function loadData() {
try {
const data = [1] fetchData();
return data;
} catch ([2]) {
console.error('Error:', error);
}
}await inside try block.Use await to wait for the promise. The catch block catches errors with a parameter, commonly named error.
Fill all three blanks to create an async arrow function that fetches JSON data.
const fetchJson = async () => {
const response = await fetch([1]);
const data = await response.[2]();
return [3];
};text() instead of json() to parse response.The fetch function needs a URL string. The response object has a json() method to parse JSON. Finally, return the parsed data.