Complete the code to wait for the promise to resolve before logging the result.
async function fetchData() {
const result = [1] fetch('https://api.example.com/data');
console.log(result);
}The await keyword pauses the function until the promise resolves, so result holds the resolved value.
Complete the code to correctly handle the resolved value from the async function.
async function getNumber() {
return 42;
}
async function showNumber() {
const num = [1] getNumber();
console.log(num);
}Using await before getNumber() waits for the promise to resolve and assigns the value 42 to num.
Complete the code to correctly handle the promise inside a non-async function.
function loadData() {
fetchData().[1]((data) => {
console.log(data);
});
}You cannot use await inside a non-async function. Instead, use then to handle the promise.
Fill both blanks to create an async function that waits for two promises and logs their sum.
async function sumAsync() {
const a = [1] fetchNumberA();
const b = [2] fetchNumberB();
console.log(a + b);
}Both a and b must await their promises to get the resolved numbers before adding.
Fill all three blanks to create an async function that fetches data, processes it, and returns the result.
async function processData() {
const data = [1] fetchData();
const processed = [2] process(data);
return [3];
}First, await the data fetch, then await the processing, and finally return the processed result.