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

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

$
0
0

2017 answer: you can now do exactly what you want in every current browser and Node.js

This is quite simple:

  • Return a Promise
  • Use the 'await', which will tell JavaScript to await the promise to be resolved into a value (like the HTTP response)
  • Add the 'async' keyword to the parent function

Here's a working version of your code:

(async function(){    var response = await superagent.get('...')    console.log(response)})()

await is supported in all current browsers and Node.js 8


Viewing all articles
Browse latest Browse all 46

Trending Articles



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