Quantcast
Channel: How do I return the response from an asynchronous call? - Stack Overflow
Viewing all articles
Browse latest Browse all 44

Answer by Zia for How do I return the response from an asynchronous call?

$
0
0

I follow these two ways Promises and async/await

Promises:

function makeAsyncCall() {  return new Promise((resolve, reject) => {    setTimeout(() => {      const response = 'Async response';      resolve(response);    }, 2000);  });}makeAsyncCall()  .then(response => {    console.log(response);   })  .catch(error => {    console.error(error);   });

And async/await:

async function makeAsyncCall() {  return new Promise((resolve, reject) => {    setTimeout(() => {      const response = 'Async response';      resolve(response);    }, 2000);  });}async function handleAsyncCall() {  try {    const response = await makeAsyncCall();    console.log(response);   } catch (error) {    console.error(error);   }}handleAsyncCall();

Viewing all articles
Browse latest Browse all 44

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>