Middle
What is the result of executing asynchronous functions?
sobes.tech AI
Answer from AI
The result of an asynchronous function is a Promise that resolves with the value returned by the function or rejects with an error.
async function getData() {
// Asynchronous operation
const result = await fetch('/api/data');
const json = await result.json();
return json; // This is the value with which the Promise will resolve
}
// Calling an async function returns a Promise
getData()
.then(data => {
// Receive the resolved value
console.log(data);
})
.catch(error => {
// Handle error
console.error(error);
});