Complete the code to log a message after 2 seconds using setTimeout.
setTimeout(() => { console.log('Hello after 2 seconds'); }, [1]);The setTimeout function expects the delay in milliseconds. 2000 ms equals 2 seconds.
Complete the code to fetch data asynchronously using fetch and log the response.
fetch('https://api.example.com/data') .then(response => response.[1]()) .then(data => console.log(data));
text() which returns plain text instead of JSON.The response.json() method parses the response body as JSON.
Fix the error in the async function to await the fetch call.
async function getData() {
const response = [1] fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);
}await causes response to be a promise, not the actual response.The await keyword pauses the function until the fetch promise resolves.
Fill both blanks to create a promise that resolves after 1 second.
const wait = () => new Promise(([1], [2]) => { setTimeout(resolve, 1000); });
callback or done.A promise executor function receives two arguments: resolve and reject.
Fill all three blanks to handle errors in an async function using try-catch.
async function fetchData() {
try {
const response = await fetch([1]);
const data = await response.[2]();
console.log(data);
} catch ([3]) {
console.error('Error:', error);
}
}The fetch function needs the URL string, response.json() parses JSON, and the catch block catches the error variable.