Quantcast
Channel: How do I return the response from an asynchronous call? - Stack Overflow
Browsing latest articles
Browse All 44 View Live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Image may be NSFW.
Clik here to view.

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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

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

2017 answer: you can now do exactly what you want in every current browser and Node.jsThis is quite simple:Return a PromiseUse the 'await', which will tell JavaScript to await the promise to be...

View Article

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

Another solution is to execute code via the sequential executor nsynjs.If the underlying function is promisifiednsynjs will evaluate all promises sequentially, and put the promise result into the data...

View Article


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

This is one of the places which two-way data binding or store concept that's used in many new JavaScript frameworks will work great for you...So if you are using Angular, React, or any other frameworks...

View Article


Answer by T.J. Crowder for How do I return the response from an asynchronous...

Most of the answers here give useful suggestions for when you have a single async operation, but sometimes, this comes up when you need to do an asynchronous operation for each entry in an array or...

View Article

Answer by Mahfuzur Rahman for How do I return the response from an...

Use a callback() function inside the foo() success.Try it in this way. It is simple and easy to understand.var lat = "";var lon = "";function callback(data) { lat = data.lat; lon = data.lon;}function...

View Article

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

Here are some approaches to work with asynchronous requests:Browser Promise objectQ - A promise library for JavaScriptA+ Promises.jsjQuery deferredXMLHttpRequest APIUsing callback concept - As...

View Article


Image may be NSFW.
Clik here to view.

Answer by Johannes Fahrenkrug for How do I return the response from an...

I will answer with a horrible-looking, hand-drawn comic. The second image is the reason why result is undefined in your code example.

View Article

Answer by Francisco Carmona for How do I return the response from an...

Have a look at this example:var app = angular.module('plunker', []);app.controller('MainCtrl', function($scope,$http) { var getJoke = function(){ return...

View Article

Answer by Vinoth Rajendran for How do I return the response from an...

You can use this custom library (written using Promise) to make a remote call.function $http(apiConfig) { return new Promise(function (resolve, reject) { var client = new XMLHttpRequest();...

View Article

Answer by Pablo Matias Gomez for How do I return the response from an...

The short answer is, you have to implement a callback like this:function callback(response) { // Here you can do what ever you want with the response object. console.log(response);}$.ajax({ url: "...",...

View Article



Answer by loretoparisi for How do I return the response from an asynchronous...

The following example I have written shows how toHandle asynchronous HTTP calls;Wait for response from each API call;Use Promise pattern;Use Promise.all pattern to join multiple HTTP calls;This working...

View Article

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

While promises and callbacks work fine in many situations, it is a pain in the rear to express something like:if (!name) { name = async1();}async2(name);You'd end up going through async1; check if name...

View Article

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

We find ourselves in a universe which appears to progress along a dimension we call "time". We don't really understand what time is, but we have developed abstractions and vocabulary that let us reason...

View Article

Answer by David R Tribble for How do I return the response from an...

Short answer: Your foo() method returns immediately, while the $ajax() call executes asynchronously after the function returns. The problem is then how or where to store the results retrieved by the...

View Article


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

Another approach to return a value from an asynchronous function, is to pass in an object that will store the result from the asynchronous function.Here is an example of the same:var async =...

View Article

Answer by Benjamin Gruenbaum for How do I return the response from an...

If you're using promises, this answer is for you.This means AngularJS, jQuery (with deferred), native XHR's replacement (fetch), Ember.js, Backbone.js's save or any Node.js library that returns...

View Article

Answer by Maleen Abewardana for How do I return the response from an...

Angular 1People who are using AngularJS, can handle this situation using promises.Here it says,Promises can be used to unnest asynchronous functions and allows one to chain multiple functions...

View Article


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

You are using Ajax incorrectly. The idea is not to have it return anything, but instead hand off the data to something called a callback function, which handles the data.That is:function handleData(...

View Article


Answer by Hemant Bavle for How do I return the response from an asynchronous...

The simplest solution is to create a JavaScript function and call it for the Ajax success callback.function callServerAsync(){ $.ajax({ url: '...', success: function(response) {...

View Article

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

XMLHttpRequest 2 (first of all, read the answers from Benjamin Gruenbaum and Felix Kling)If you don't use jQuery and want a nice short XMLHttpRequest 2 which works in the modern browsers and also in...

View Article

Answer by Benjamin Gruenbaum for How do I return the response from an...

If you're not using jQuery in your code, this answer is for youYour code should be something along the lines of this:function foo() { var httpRequest = new XMLHttpRequest(); httpRequest.open('GET',...

View Article

Answer by Felix Kling for How do I return the response from an asynchronous...

→ For a more general explanation of asynchronous behaviour with different examples, seeWhy is my variable unaltered after I modify it inside of a function? - Asynchronous code reference→ If you already...

View Article


How do I return the response from an asynchronous call?

How do I return the response/result from a function foo that makes an asynchronous request?I am trying to return the value from the callback, as well as assigning the result to a local variable inside...

View Article

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

In JS, when an asynchronous function is called, it returns a Promise. If you want to return a value from the function, you need to ensure you are properly handling this Promise.There is an...

View Article

Browsing latest articles
Browse All 44 View Live




Latest Images