This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 months ago.
Hi I'm trying to get JSON data from a JSON file. I got it on my JS code but i cant use it out of the callback function. I want to use the data anywhere in my code. I want to pass the data to a variable
My code for getting JSON data is :
async function load() {
let url = 'sorular.json';
let obj;
obj = await (await fetch(url)).json();
return obj
}
load().then(res=>{console.log(res)})
in the THEN, simply call a separate function where res is passed as argument.
function processJSON(data){
//run your other functions here
}
load().then(res=>{processJSON(res)})
Related
This question already has answers here:
How can I access the value of a promise?
(14 answers)
How to return value from an asynchronous callback function? [duplicate]
(3 answers)
Closed 9 months ago.
I am using js fetch request.
I have this inside a function, when I call the function I want it to return my data.
This is not happening.
It would appear that the promise is not getting 'resolved'.
I clearly don't fully understand promises, so any advice on that in relation to the below example would be very useful.
Here is my sample code:
const test = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/todos')
const data = await response.json()
return data;
}
console.log(test())
Output:
// [object promise]
{}
Question: How can I write the fetch request so that when I run test(), it returns me the json as I would expect?
Thank you
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
All I basically want to do is to be able to use the return value from an asynchronous function. I assumed the variable tag (in the code below) would contain the return value from the async function but it rather contains the response. How could I possibly get hold of the return value from the aRes function into a variable that I can use outside the scope of the async function. Thank you in advance.
var http = require('http');
var url = "someurl.com"
//async function to be passed to http.get as second parameter
function aRes(response){
var newtag = response.headers.etag;
return newtag;
}
var tag = http.get(url,aRes);
console.log(tag); //logs the response from get request
//tag is not the return value from the ares function but rather the response object. How do I get the return value of that?
Use callback. Or better yet use Promise (recommend trying out bluebird), your code will look easier to read.
You can do something like below with request library after promisify it.
request.getAsync(url)
.spread(yourFunction)
yourFunction will take args from requestAsync.
Create your own callback function:
var http = require('http');
var url = "someurl.com";
var aResCallback = function(tag) {
console.log(tag); //logs the response from get request
};
//async function to be passed to http.get as second parameter
function aRes(response){
var newtag = response.headers.etag;
aResCallback(newtag);
return newtag;
}
var tag = http.get(url, aRes);
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 7 years ago.
Right now I have three files, two javascript files and one html file that will call upon these two javascript files. Inside my first javascript file, let's call it part1.js, I have this content:
var dataval;
$.get('example.cgi', function(data){
dataval = data;
});
Inside part2.js, I am trying to call on this dataval value. But it is not working, as I am assuming that dataval is inside the get function and is therefore a local value. Is there anyway I can grab that value? My html file is calling on both of these javascripts. Is there anyway I can access that variable?
Thanks!!
There is a problem here with asynchronous calls. You should either use a callback function or make use of the jquery promise capability.
In your part1.js, you should instead define a function:
function callCgi(callback) {
$.get('example.cgi', callback); //Callback will receive the data.
}
and then use it in part2.js:
callCgi(function(data) {
//do something with data.
});
Or with promises:
function callCgi() {
return $.get('example.cgi');
}
and then use it in part2.js:
callCgi().then(function(data) {
//do something with data.
});
This question already has answers here:
Ajax jquery async return value
(3 answers)
Closed 7 years ago.
I have the following code:
Main.User = (function(){
var currentUser = ''
var get_current_user = function get_current_user(){
Main.Mod.do_ajax('home/get_user_pnp_id','GET',function(data){
currentUser = data.username;
},null);
console.log(currentUser); //Doesn't work. It logs empty string.
return currentUser;
}
return {
get_current_user : get_current_user,
}
})();
The 3rd parameter from Main.Mod.do_ajax() is a callback success function from my ajax call. However I cannot set currentUser inside that callback function.
What seems to be the problem?
It's because your callback hasn't been called yet when the assignment is made since the callback is asynchronous. See Ajax jquery async return value and http://node-tricks.com/return-response-ajax-call/
This question already has answers here:
Saving data from the d3.js ajaxrequest in a variable to create axis with data
(2 answers)
Closed 7 years ago.
I have a problem with this snippet:
d3.csv("data/airports.csv", function(err, a) {
var count=0;
a.forEach(function(i){
if(i.iata_faa == ""){}
else {
count++;
addpoint(i.lon, i.lat,i);
}
});
airports=a;
myDataIsReady();
console.log(count);
});
function myDataIsReady(){
console.log(airports);
return airports;
}
console.log(airports);
Notice that airports is a global variable here.
I need to handle the variable airports for another function, but the value is null, I think that is null because the csv file has not yet been processed completely, right?
How I resolve it?
Generally for async functions, you push a callback (a function reference) into the async method, so that it processes the data when the ajax call completes. You don't return data from that type function, you inject data into it.