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

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

$
0
0

Here is an example that works:

const validateName = async userName => {  const url = "https://jsonplaceholder.typicode.com/todos/1";  try {    const response = await axios.get(url);    return response.data  } catch (err) {    return false;  }};validateName("user") .then(data => console.log(data)) .catch(reason => console.log(reason.message))
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.21.1/axios.min.js"></script>

Viewing all articles
Browse latest Browse all 44

Trending Articles



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