Call function inside a function angularJS [duplicate] - javascript

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Why are AngularJS $http success/error methods deprecated? Removed from v1.6?
(2 answers)
Closed 5 years ago.
I have function in angularJS invoked when clicked, I am also calling another service function inside that function, which is going to change some values for later use in first function.
Problem statement: Function getData finishes first with retrning model, and after it finishes it all process then calll getDetails. I want to make this call in sequence.
this.getData = function () {
var a = '';
var b = '';
var c = '';
//I want this call to be finished before above function
MyServices.getDetails(id, type)
.success(function(data, status, headers, config){
if(status == '200'){
// my code goes here to change some values
})
}
var model = [];
retrun model
};

Related

Callback function for storing JSON to a variable [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 2 years ago.
I had a previous question where I was having issues for getting json from an url and storing it to a variable in order to pass it further to a function. I managed to get redirected to a solution that seemed promising, but it's still giving me undefined. The proposal was to use a callback as follows, but it doesn't seem to do the job.
var space = "";
function spaceCallback(data) {
fplan = data.space['space'];
}
function getSpace() {
var url = "http://localhost:8000/spaces/send_space/"
$.getJSON(url, function (data) {
spaceCallback(data);
});
}
I would need to pass space to blueprint3d.model.loadSerialized(space);.
What am I missing here?
Is it possible that you need to return your function?
function getSpace() {
var url = "http://localhost:8000/spaces/send_space/"
$.getJSON(url, function (data) {
return spaceCallback(data);
});
}
or ES6:
function getSpace() {
var url = "http://localhost:8000/spaces/send_space/"
$.getJSON(url, (data) => spaceCallback(data));
}

Loop through an array of urls and pass them to get_json, return the new array of datas [duplicate]

This question already has answers here:
How to wait until jQuery ajax request finishes in a loop?
(5 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
I can't figure how to loop through an array of urls and pass them to getJSON, to finally return the new array of datas.
var data_num=[];
$.each(arr_urls, function( index, value ) {
$.getJSON(arr_urls[index], function (data) {});
data_num.push(data);
});
alert(data_num);
var data_num=[];
$.each(arr_urls, function( index, value ) {
$.getJSON(arr_urls[index], function (data) {
data_num.push(data);
alert(data_num);
});
});
Put your pusher and alert in the callback like this.
Why can't you just do:
var jsonData = JSON.stringify(arr_urls);

Factory that returns data after asynchronous call [duplicate]

This question already has answers here:
How to return value from an asynchronous callback function? [duplicate]
(3 answers)
Closed 8 years ago.
I have a service(factory) that uses another service to fetch data.Something like this:
factory('myFactory', function(anotherFactory) {
var factoryObject = {};
factoryObject.myMethod = () {
var newObjectToReturn;
// async call
anotherFactory.get(id, function(data) {
// do some processing
newObjectToReturn = data;
});
return newObjectToReturn;
}
return factoryObject;
});
Now, problem is of course, that because of asynchronous call, factoryObject.myMethod() always returns undefined, because return newObjectToReturn is first executed, and I can't simply return data. Is there any way around this ?
return the response data in callback
factory('myFactory', function(anotherFactory) {
var factoryObject = {};
factoryObject.myMethod = () {
var newObjectToReturn;
// async call
anotherFactory.get(id, function(data) {
// do some processing
newObjectToReturn = data;
},function(response){
return newObjectToReturn;
});
}
return factoryObject;
});

JavaScript variable not being set [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Variables set during $.getJSON function only accessible within function
(6 answers)
Closed 8 years ago.
I have a small piece of js that pull some data from via ajax.
solarSystem = "test";
$.getJSON( "ajax/getLocation.php", function( data ) {
var items = [];
$.each( data, function( key, val ) {
solarSystem = val;
});
});
alert(solarSystem);
val is being set but its not outputting to the alert if i put the alert in that function it works. I assume its something to do with scope but im new to js if anyone can point me in the right direction it would be much apreciated.
While JavaScript is single-threaded, it can perform tasks asynchronously, i.e. function calls do not necessarily complete before the next line of code. AJAX (asynchronous JavaScript and XML) calls do just that. For example,
console.log("starting")
$.getJSON("path/to/resource", function (data) {
console.log("data retrieved")
})
console.log("finished")
The above code will most likely print starting and finished before it prints data retrieved. This is because $.getJSON is asynchronous. It requests a resource on a server somewhere, waits for a response, then calls the anonymous function provided as a callback, i.e. the function (data) { /* etc */ } part.
In your case, the callback function is running after alert, even though it is written above it in your code. If you want to alert(solarSystem), you'll need to place that inside of your $.getJSON call to ensure it is processed correctly:
solarSystem = "test"
$.getJSON("ajax/getLocation.php", function (data) {
var items = []
$.each(data, function (key, val) {
solarSystem = val
})
alert(solarSystem)
})
Anything inside the $.getJSON cannot access anything outside without some changes to your code. You need what is a called a callback function, and the problem is due to variable scoping.
solarSystem = "test";
function setSolarSystem(newSystem){
solarSystem = newSystem;
alert(solarSystem);
}
$.getJSON( "ajax/getLocation.php", function( data ) {
var items = [];
$.each( data, function( key, val ) {
setSolarSystem(val);
});
});

How to return data from JQuery GET [duplicate]

This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 9 years ago.
I made a function that runs a get request using JQuery.
But I need the get to return this received value to the calling function. function
gettime() {
var timeOut =0;
$.get("http://localhost:8080/t.html", function( data )
{
timeOut = data*1000;
return timeOut;
});
//retun(timeOut);
}
I want the value received by get to be returned to the main function that calls gettime()
please help out.
It's a integer am passing.
An idea could be implementing a callback:
function gettime(callback) {
var timeOut =0;
$.get("http://localhost:8080/t.html", function(data)
{
timeOut = data*1000;
callback(timeOut);
});
}
//Then you can retrieve that value by doing this:
gettime(function(timeout){
//Do your stuff here.
});

Categories

Resources