How can I retrieve the result of an asynchronous operation

0 votes
With the help of a proper explanation and code, can you tell me How I can retrieve the result of an asynchronous operation?
Jan 9 in Java-Script by Ashutosh
• 14,020 points
29 views

1 answer to this question.

0 votes

You can retrieve the result of an asynchronous operation in JavaScript using Promises or async/await.

1. Using Promises:

fetch('https://api.example.com/data')

  .then(response => response.json()) // Process the response

  .then(data => console.log(data))   // Handle the data

  .catch(error => console.error('Error:', error)); // Handle errors

2. Using async/await (Preferred for readability):

async function fetchData() {

  try {

    const response = await fetch('https://api.example.com/data'); // Wait for fetch

    const data = await response.json(); // Wait for JSON parsing

    console.log(data); // Use the data

  } catch (error) {

    console.error('Error:', error); // Handle errors

  }

}

fetchData();

answered Jan 10 by Navya

Related Questions In Java-Script

0 votes
1 answer

How can I check the existence of an element in jQuery?

Hello @ Arpit In JavaScript, everything is 'truthy' or ...READ MORE

answered Sep 8, 2020 in Java-Script by Niroj
• 82,840 points
926 views
0 votes
1 answer

How can I determine the type of an HTML element in JavaScript?

Hello @kartik, nodeName is the attribute you are looking ...READ MORE

answered Oct 8, 2020 in Java-Script by Niroj
• 82,840 points
2,505 views
0 votes
1 answer

How do I return the response from an asynchronous call?

Hello @kartik, You are using Ajax incorrectly. The ...READ MORE

answered Jun 17, 2020 in Java-Script by Niroj
• 82,840 points
1,069 views
0 votes
1 answer

How can I get the user's local time instead of the server's time?

Hello @kartik, For client side, you would need ...READ MORE

answered Jul 7, 2020 in Java-Script by Niroj
• 82,840 points
710 views
0 votes
1 answer

How can I configure lazy loading for Angular modules?

To configure lazy loading in Angular, you ...READ MORE

answered Dec 12, 2024 in Angular by Navya
56 views
0 votes
1 answer

How do I use ES6 features like destructuring in a Node.js application?

To use ES6 features like destructuring in ...READ MORE

answered Dec 17, 2024 in Node-js by Navya
64 views
0 votes
1 answer

How can I iterate through the properties of a JavaScript object?

You can iterate through the properties of ...READ MORE

answered Jan 10 in Java-Script by Navya
53 views
0 votes
1 answer

How to sum the values of a javascript object

Certainly, I'd be happy to help you ...READ MORE

answered Sep 25, 2023 in Java-Script by Edureka
• 12,690 points
4,480 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP