Answer by Zia for How do I return the response from an asynchronous call?
I follow these two ways Promises and async/awaitPromises:function makeAsyncCall() { return new Promise((resolve, reject) => { setTimeout(() => { const response = 'Async response';...
View ArticleAnswer by Lionel Yeo for How do I return the response from an asynchronous call?
Many answers do not answer how to obtain the variable inside the callback function and use it later on.var myvar = '';var myvar2 = ajax("GET", "/test", "acrive=1").then(function(result) { myvar =...
View ArticleAnswer by Henke for How do I return the response from an asynchronous call?
1. A first stumbling stepAs for many others, my encounter with asynchronous calls was puzzling at first. I don't remember the details, but I may have tried something like:let result;$.ajax({ url:...
View ArticleAnswer by Abd Abughazaleh for How do I return the response from an...
async: falseI solved it by setting async to false and restructure my Ajax call:I set a global function called sendRequest(type, url, data) with three parameters to be called every time...
View ArticleAnswer by Philipp Claßen for How do I return the response from an...
Originally, callbacks were used for asynchronous operations (e.g., in the XMLHttpRequest API). Now promise-based APIs like the browser's Fetch API have become the default solution and the nicer...
View ArticleAnswer by Faiz Mohammed for How do I return the response from an asynchronous...
There is no way you can directly return the result of an Ajax response from a function. The reason is that an Ajax call ($.get() or $.post()) is asynchronous and calling the function that encapsulates...
View ArticleAnswer by SanjiMika for How do I return the response from an asynchronous call?
After reading all the responses here and with my experiences, I would like to resume the detail of callback, promise and async/await for the asynchronous programming in JavaScript.1) Callback: The...
View ArticleAnswer by nonopolarity for How do I return the response from an asynchronous...
I think no matter what method or mechanism used, or whatever the framework is (Angular/React) that hides it from you, the following principle holds:In the flow of the program (think code or even the...
View ArticleAnswer by Kamil Kiełczewski for How do I return the response from an...
AwaitA request works in an asynchronous way, so you can't read the data synchronously as in typical code. However, using async/await you can create asynchronous code which looks close/similar to the...
View ArticleAnswer by Murtaza Hussain for How do I return the response from an...
Use of async/await with a transpilers like Babel to get it working in older browsers. You’ll also have to install this Babel preset and polyfill from npm: npm i -D babel-preset-env...
View ArticleAnswer by Amir Fo for How do I return the response from an asynchronous call?
Using PromiseThe most perfect answer to this question is using Promise.function ajax(method, url, params) { return new Promise(function(resolve, reject) { var xhr = new XMLHttpRequest(); xhr.onload =...
View ArticleAnswer by Alex Montoya for How do I return the response from an asynchronous...
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 }...
View ArticleAnswer by Matthew Brent for How do I return the response from an asynchronous...
Rather than throwing code at you, there are two concepts that are key to understanding how JavaScript handles callbacks and asynchronicity (is that even a word?)The Event Loop and Concurrency...
View ArticleAnswer by James for How do I return the response from an asynchronous call?
ECMAScript 6 has 'generators' which allow you to easily program in an asynchronous style.function* myGenerator() { const callback = yield; let [response] = yield $.ajax("https://stackoverflow.com",...
View ArticleAnswer by Aniket Jha for How do I return the response from an asynchronous call?
JavaScript is single threaded.The browser can be divided into three parts:Event LoopWeb APIEvent QueueThe event loop runs for forever, i.e., kind of an infinite loop. The event queue is where all your...
View ArticleAnswer by Fernando Carvajal for How do I return the response from an...
Using ES2017 you should have this as the function declaration.async function foo() { var response = await $.ajax({url: '...'}) return response;}And executing it like this.(async function() { try { var...
View ArticleAnswer by Pieter Jan Bonestroo for How do I return the response from an...
The question was:How do I return the response from an asynchronous call?which can be interpreted as:How to make asynchronous code look synchronous?The solution will be to avoid callbacks, and use a...
View ArticleAnswer by Anish K. for How do I return the response from an asynchronous call?
It's a very common issue we face while struggling with the 'mysteries' of JavaScript. Let me try demystifying this mystery today.Let's start with a simple JavaScript function:function foo(){ // Do...
View ArticleAnswer by Haim Zamir for How do I return the response from an asynchronous call?
Let's see the forest first before looking at the trees.There are many informative answers with great details here, I won't repeat any of them. The key to programming in JavaScript is having first the...
View ArticleAnswer by Khoa Bui for How do I return the response from an asynchronous call?
Of course there are many approaches like synchronous request, promise, but from my experience I think you should use the callback approach. It's natural to asynchronous behavior of JavaScript.So, your...
View Article