fail to call a function in another one with AngularJS - javascript

I want to call a function in another one.
The function to be called returns true or false:
$scope.verifsubdir = function(dir, tocheck) {
$http({method: 'GET', url: '/checkIsSubdir/'+dir+'/'+tocheck})
.success(function(result) {
return result.data;
})
.error(function(result) {
console.log("checksubdir oops");
});
};
The function call in another function:
$scope.CopyFiles = function(destination) {
for(var i=0; i<$scope.list.length; i++) {
console.log($scope.verifsubdir($scope.list[i],destination));
};
}
But I always get undefined.
However, when I merge the two functions I get true or false:
$scope.CopyFiles=function(destination){
for(var i=0; i<$scope.list.length; i++){
$http({method: 'GET', url:
'/checkIsSubdir/'+$scope.list[i]+'/'+destination})
.success(function(result) {
console.log("checkdir "+result.data);
})
.error(function(data, status, headers, config) {
console.log("checksubdir oops");
});
}
};
I want them to be separated. How to fix this?

In the $http call returns a promise, your should do something like this:
$scope.verifsubdir($scope.list[i],destination).then(function(resp) {
console.log(resp);
// assuming the response returns a boolean
$scope.list[i].verified = resp
});

Using $http from Angular gives you a Promise back (you can read more about them in the documentation for $q), and won't make your function return any value on its own (this is why you get undefined).
Your first function will need to return the promise and then you would need to add additional .success(...) or .error(...) callbacks inside your CopyFiles function:
$scope.verifsubdir = function (dir, tocheck) {
// Simply add return in front of $http here.
return $http(...
};
$scope.CopyFiles = function (destination) {
for (var i = 0; i < $scope.list.length; i++) {
$scope.verifsubdir($scope.list[i], destination).success(function (result) {
console.log("checkdir " + result.data);
});
}
};

Related

$http request to return all records without pagination in a single request

There are more than 4000 records. API returns max 1000 records and has pagination. I call for this function (loop), and use "skip" to obtain records at intervals of 1000 records.
I need all records at once, but below code returns only first 1000 records.
var array=[];
function loop(skip){
return $http({
method: 'GET',
url: myurl+'&$skip='+skip,
timeout:5000
}).success(function(res){
if(res.d.length>0)
{
Array.prototype.push.apply( array,res.d);
loop(skip +1000);
}
return array;
}).error(function(response,status,headers,config) {
});
}
getAll = function() {
return loop(0);
}
I need a single request can obtain the total records.
but only I get the first 1000 records in this part :(
getAll().then(function() {
console.log("in this part i need the array with my 4000 records")
});
First, a bit sidenote from angular doc:
The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.
Yet another solution: pass accumulator result as parameter to loop function
function loop(skip,result){
result = result||[];//accumulator for result, init empty array if not pass
return $http(
{
method: 'GET',
url: myurl+'&$skip='+skip,
timeout:5000
}).then(function success(response){
if(response.data.length > 0){
Array.prototype.push.apply(result,response.data);
return loop(skip+1000,result);
}
return result;
},function error(){
});
}
Notice, that main difference from your current code is return before calling loop function inside success handler.
This work, because if from then function return promise, then next then would be applied after returned promise fulfiled.
Sample plunkr
Should be possible with some recursive promise chaining. Try this
function getAll(page) {
if (typeof page === 'undefined') {
page = 0;
}
return $http.get(myurl, {$skip: page * 1000}).then(function(res) {
var data = res.data;
if (data.length > 0) {
return getAll(page + 1).then(function(nextData) {
return data.concat(nextData);
});
}
return data;
});
}
And call it like
getAll().then(function(allRecords) {
console.log(allRecords);
});
Here's a hacked together Plunker demo ~ http://plnkr.co/edit/ey8gdytvuBE6cpuMAtnB?p=preview
The
Working sample https://jsfiddle.net/coolbhes/xmqur1bq/ (replaced service with a promise)
Code which would work for you as below :
var array = [];
function loop(skip, resolveHandle) {
$http({
method: 'GET',
url: myurl + '&$skip=' + skip,
timeout: 5000
}).then(function successCallback(res) {
if (res.d.length > 0) {
Array.prototype.push.apply(array, res.d);
loop(skip + 1000, resolveHandle);
} else {
//done with all requests;
resolveHandle(array);
}
}, function errorCallback(rej) {
});
};
function getAll() {
return new Promise(function(resolve, reject) {
loop(0, resolve);
});
}
getAll().then(function(data) {
console.log("in this part i need the array with my 4000 records", data)
}

Angularjs Factory deferred's data disapearing

I'm trying to do a caching factory for http requests, so it doesn't make the server do a lot of work for the same request. But It seems my way of using deferred "swallows" the data, and I don't know why.
Console output for below:
data fetched:
Object {state: "OK", data: Object, errorMessage: null, exception: null}
success
undefined
ImportFactory:
factory("importFactory", function ($http, $q, loggingService) {
return{
fetchedData: [],
cacheTransport: function (transportsId, data) {
this.fetchedData.push({"transportsId": transportsId, "data": data});
},
getImport: function (transportsId) {
var factory = this;
var deferred = $q.defer();
var preFetchedTransport = this.findTransport(transportsId);
if (preFetchedTransport === null) {
console.log('fetching from backend');
return $http.post("/import/create/" + transportsId).then(function (data) {
console.log('data fetched:');
console.log(data);
factory.cacheTransport(transportsId, data);
deferred.resolve(data);
});
}
preFetchedTransport = deferred.promise;
return preFetchedTransport;
},
findTransport: function (transportsId) {
for (var i = 0; i < this.fetchedData.length; i++) {
var transportObj = this.fetchedData[i];
if (transportObj.transportsId === transportsId) {
return transportObj.data;
}
}
return null;
}
};
});
Controller
.controller('ImportController', function ($scope, $routeParams, importFactory){
$scope.transportId = $routeParams.id;
importFactory.getImport($scope.transportId).then(function (successData) {
console.log('success');
console.log(successData);
}, function (errorData) {
console.log('error');
console.log(errorData);
});
You basically need this: Demo here.
var cachedPromises = {};
return {
getStuff: function(id) {
if (!cachedPromises[id]) {
cachedPromises[id] = $http.post("/import/create/" + id).then(function(resp) {
return resp.data;
});
}
return cachedPromises[id];
}
};
Now, when you fetch that data, you can manipulate and it will be changed when you access it in the future.
myService.getStuff(whatever).then(function(data) {
data.foo = 'abc';
});
//elsewhere
myService.getStuff(whatever).then(function(data) {
console.log(data.foo); // 'abc'
});
Here's a demo that does this, as well as a view updating trick (bind the object to the view before the data comes in), and an idea of how you could change the data separately from the cache, in case you want to have the original data and the changing data. http://jsbin.com/notawo/2/edit
Remember to avoid that nasty promise anti-pattern. If you already have a promise, use that instead of creating another with $q. $http already returns a promise and that promise is sufficient for whatever you need if you use it properly.
just change the loop condition look like this and then test i think your function and defer is work fine but the loop does not sent the correct data
for(var i = 0; i < this.fetchedData.length; i++) {
if (this.fetchedData[i].transportsId === transportsId) {
return this.fetchedData[i].data;
}
}
return null;
}
The reason you are getting undefined is you are not returning anything from the $http.post().then() !
Also in your getImport() function you are returning an empty promise when the transport is already cached. You need to resolve it to your already cached transport object.
getImport: function (transportsId) {
var factory = this;
var deferred = $q.defer();
var preFetchedTransport = this.findTransport(transportsId);
if (preFetchedTransport === null) {
console.log('fetching from backend');
return $http.post("/import/create/" + transportsId).then(function (data) {
console.log('data fetched:');
console.log(data);
factory.cacheTransport(transportsId, data);
return data; //this was missing
});
}
// resolve it with transport object if cached
deferred.resolve(preFetchedTransport);
return deferred.promise;
},

Multiple Promise Chains in Single Function

I have some code that will dynamically generate an AJAX request based off a scenario that I'm retrieving via an AJAX request to a server.
The idea is that:
A server provides a "Scenario" for me to generate an AJAX Request.
I generate an AJAX Request based off the Scenario.
I then repeat this process, over and over in a Loop.
I'm doing this with promises here: http://jsfiddle.net/3Lddzp9j/11/
However, I'm trying to edit the code above so I can handle an array of scenarios from the initial AJAX request.
IE:
{
"base": {
"frequency": "5000"
},
"endpoints": [
{
"method": "GET",
"type": "JSON",
"endPoint": "https://api.github.com/users/alvarengarichard",
"queryParams": {
"objectives": "objective1, objective2, objective3"
}
},
{
"method": "GET",
"type": "JSON",
"endPoint": "https://api.github.com/users/dkang",
"queryParams": {
"objectives": "objective1, objective2, objective3"
}
}
]
This seems like it would be straight forward, but the issue seems to be in the "waitForTimeout" function.
I'm unable to figure out how to run multiple promise chains. I have an array of promises in the "deferred" variable, but the chain only continues on the first one--despite being in a for loop.
Could anyone provide insight as to why this is? You can see where this is occuring here: http://jsfiddle.net/3Lddzp9j/10/
The main problems are that :
waitForTimeout isn't passing on all the instructions
even if waitForTimeout was fixed, then callApi isn't written to perform multiple ajax calls.
There's a number of other issues with the code.
you really need some data checking (and associated error handling) to ensure that expected components exist in the data.
mapToInstruction is an unnecessary step - you can map straight from data to ajax options - no need for an intermediate data transform.
waitForTimeout can be greatly simplified to a single promise, resolved by a single timeout.
synchronous functions in a promise chain don't need to return a promise - they can return a result or undefined.
Sticking with jQuery all through, you should end up with something like this :
var App = (function ($) {
// Gets the scenario from the API
// sugar for $.ajax with GET as method - NOTE: this returns a promise
var getScenario = function () {
console.log('Getting scenario ...');
return $.get('http://demo3858327.mockable.io/scenario2');
};
var checkData = function (data) {
if(!data.endpoints || !data.endpoints.length) {
return $.Deferred().reject('no endpoints').promise();
}
data.base = data.base || {};
data.base.frequency = data.base.frequency || 1000;//default value
};
var waitForTimeout = function(data) {
return $.Deferred(function(dfrd) {
setTimeout(function() {
dfrd.resolve(data.endpoints);
}, data.base.frequency);
}).promise();
};
var callApi = function(endpoints) {
console.log('Calling API with given instructions ...');
return $.when.apply(null, endpoints.map(ep) {
return $.ajax({
type: ep.method,
dataType: ep.type,
url: ep.endpoint
}).then(null, function(jqXHR, textStatus, errorThrown) {
return textStatus;
});
}).then(function() {
//convert arguments to an array of results
return $.map(arguments, function(arg) {
return arg[0];
});
});
};
var handleResults = function(results) {
// results is an array of data values/objects returned by the ajax calls.
console.log("Handling data ...");
...
};
// The 'run' method
var run = function() {
getScenario()
.then(checkData)
.then(waitForTimeout)
.then(callApi)
.then(handleResults)
.then(null, function(reason) {
console.error(reason);
})
.then(run);
};
return {
run : run
}
})(jQuery);
App.run();
This will stop on error but could be easily adapted to continue.
I'll try to answer your question using KrisKowal's q since I'm not very proficient with the promises generated by jQuery.
First of all I'm not sure whether you want to solve the array of promises in series or in parallel, in the solution proposed I resolved all of them in parallel :), to solve them in series I'd use Q's reduce
function getScenario() { ... }
function ajaxRequest(instruction) { ... }
function createPromisifiedInstruction(instruction) {
// delay with frequency, not sure why you want to do this :(
return Q.delay(instruction.frequency)
.then(function () {
return this.ajaxRequest(instruction);
});
}
function run() {
getScenario()
.then(function (data) {
var promises = [];
var instruction;
var i;
for (i = 0; i < data.endpoints.length; i += 1) {
instruction = {
method: data.endpoints[i].method,
type: data.endpoints[i].type,
endpoint: data.endpoints[i].endPoint,
frequency: data.base.frequency
};
promises.push(createPromisifiedInstruction(instruction));
}
// alternative Q.allSettled if all the promises don't need to
// be fulfilled (some of them might be rejected)
return Q.all(promises);
})
.then(function (instructionsResults) {
// instructions results is an array with the result of each
// promisified instruction
})
.then(run)
.done();
}
run();
Ok let me explain the solution above:
first of all assume that getScenario gets you the initial json you start with (actually returns a promise which is resolved with the json)
create the structure of each instruction
promisify each instruction, so that each one is actually a promise whose
resolution value will be the promise returned by ajaxRequest
ajaxRequest returns a promise whose resolution value is the result of the request, which also means that createPromisifiedInstruction resolution value will be the resolution value of ajaxRequest
Return a single promise with Q.all, what it actually does is fulfill itself when all the promises it was built with are resolved :), if one of them fails and you actually need to resolve the promise anyways use Q.allSettled
Do whatever you want with the resolution value of all the previous promises, note that instructionResults is an array holding the resolution value of each promise in the order they were declared
Reference: KrisKowal's Q
Try utilizing deferred.notify within setTimeout and Number(settings.frequency) * (1 + key) as setTimeout duration; msg at deferred.notify logged to console at deferred.progress callback , third function argument within .then following timeout
var App = (function ($) {
var getScenario = function () {
console.log("Getting scenario ...");
return $.get("http://demo3858327.mockable.io/scenario2");
};
var mapToInstruction = function (data) {
var res = $.map(data.endpoints, function(settings, key) {
return {
method:settings.method,
type:settings.type,
endpoint:settings.endPoint,
frequency:data.base.frequency
}
});
console.log("Instructions recieved:", res);
return res
};
var waitForTimeout = function(instruction) {
var res = $.when.apply(instruction,
$.map(instruction, function(settings, key) {
return new $.Deferred(function(dfd) {
setTimeout(function() {
dfd.notify("Waiting for "
+ settings.frequency
+ " ms")
.resolve(settings);
}, Number(settings.frequency) * (1 + key));
}).promise()
})
)
.then(function() {
return this
}, function(err) {
console.log("error", err)
}
, function(msg) {
console.log("\r\n" + msg + "\r\nat " + $.now() + "\r\n")
});
return res
};
var callApi = function(instruction) {
console.log("Calling API with given instructions ..."
, instruction);
var res = $.when.apply(instruction,
$.map(instruction, function(request, key) {
return request.then(function(settings) {
return $.ajax({
type: settings.method,
dataType: settings.type,
url: settings.endpoint
});
})
})
)
.then(function(data) {
return $.map(arguments, function(response, key) {
return response[0]
})
})
return res
};
var handleResults = function(data) {
console.log("Handling data ..."
, JSON.stringify(data, null, 4));
return data
};
var run = function() {
getScenario()
.then(mapToInstruction)
.then(waitForTimeout)
.then(callApi)
.then(handleResults)
.then(run);
};
return {
// This will expose only the run method
// but will keep all other functions private
run : run
}
})($);
// ... And start the app
App.run();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
jsfiddle http://jsfiddle.net/3Lddzp9j/13/
You have a return statement in the loop in your waitForTimeout function. This means that the function is going to return after the first iteration of the loop, and that is where you are going wrong.
You're also using the deferred antipattern and are using promises in places where you don't need them. You don't need to return a promise from a then handler unless there's something to await.
The key is that you need to map each of your instructions to a promise. Array#map is perfect for this. And please use a proper promise library, not jQuery promises (edit but if you absolutely must use jQuery promises...):
var App = (function ($) {
// Gets the scenario from the API
// NOTE: this returns a promise
var getScenario = function () {
console.log('Getting scenario ...');
return $.get('http://demo3858327.mockable.io/scenario');
};
// mapToInstructions is basically unnecessary. each instruction does
// not need its own timeout if they're all the same value, and you're not
// reshaping the original values in any significant way
// This wraps the setTimeout into a promise, again
// so we can chain it
var waitForTimeout = function(data) {
var d = $.Deferred();
setTimeout(function () {
d.resolve(data.endpoints);
}, data.base.frequency);
return d.promise();
};
var callApi = function(instruction) {
return $.ajax({
type: instruction.method,
dataType: instruction.type,
url: instruction.endPoint
});
};
// Final step: call the API from the
// provided instructions
var callApis = function(instructions) {
console.log(instructions);
console.log('Calling API with given instructions ...');
return $.when.apply($, instructions.map(callApi));
};
var handleResults = function() {
var data = Array.prototype.slice(arguments);
console.log("Handling data ...");
};
// The 'run' method
var run = function() {
getScenario()
.then(waitForTimeout)
.then(callApis)
.then(handleResults)
.then(run);
};
return {
run : run
}
})($);
App.run();

Angular promises: get data from buffer if available

In this scenario the requirement is to get the data with an Http request if the data is not in a buffer. If it's in the buffer, use it from there without the Http request.
I tried the code below but it doesn't make much sense; if the data is in the buffer I don't know if I should return from the function doing nothing or return the deferred promise. Any thoughts?
var dataBuffer = null;
var getData = function() {
var deferred = $q.defer();
if (dataBuffer != null) { // this is the part I'm not convinced
deferred.resolve();
return;
}
$http.get('/some/url/')
.success(function(data) {
dataBuffer = data;
deferred.resolve();
})
.error(function(data) {
deferred.reject();
});
return deferred.promise;
};
Invoked in the following way:
var promise = getData();
promise.then (
function(response) {
dataBuffer = .... // dataBuffer contains data
}
);
There is a clean simple way to use promises when you're not sure which is the code you're executing is asynchronous or not and it's using $q.when
So the code can be:
var getData = function() {
return $q.when(dataBuffer ? dataBuffer: $http.get('/some/url'))
};
Then when calling getData you can use the same code you posted or just simply:
getData()
.then(function(response){//...
})
.catch(function(err){//..
});
Beware of the deferred antipattern. You can accomplish what you are trying to do very cleanly, like this:
var dataBuffer;
var getData = function() {
if (dataBuffer) {
// return a resolved promise for dataBuffer if it is already populated
return $q.when(dataBuffer);
}
$http.get('/some/url/')
.then(function (data) {
dataBuffer = data.data;
return dataBuffer;
});
};
getData().then(function (data) {
// data contains the data you want
})
.catch(function (error) {
// error occurred.
});
dataBuffer should not be accessed outside of your getData function. To make this perfectly clear, you can wrap them together in an IIFE, although this is optional:
var getData = (function () {
var dataBuffer;
return function() {
if (dataBuffer) {
// return a resolved promise for dataBuffer if it is already populated
return $q.when(dataBuffer);
}
$http.get('/some/url/')
.then(function (data) {
dataBuffer = data.data;
return dataBuffer;
});
};
})();
getData().then(..etc etc etc...);
As a final note, remember that you can use $http's built-in caching features, and not have to reinvent the wheel with your own buffers:
// much simpler, isn't it?
var getData = function() {
$http.get('/some/url/', { cache: true }) // enable caching
.then(function (data) { return data.data });
};
getData().then(...etc etc etc...);
Why dont you enable cache instead of handling the buffer manually.
$http.get('/some/url/',{ cache: true})
.success(function(data) {
deferred.resolve(data);
})
.error(function(data) {
deferred.reject();
});

How can I use jQuery promises when referring to methods of an object?

I am trying to make multiple ajax requests and then display some content only after all ajax requests have been made. I know I should use jQuery promises but I'm not entirely sure how they work.
Here is my code:
//make first ajax request
$.ajax({
url: 'http://api.com',
success: function(result) {
var promises = [];
//for each result
for(var i = 0; i < result.length; i++) {
//make another ajax request using
//some of the returned data and
//define it as a promise
var promise = $.ajax({
url: 'http://api.com&param='+result.paramVal,
success: function(result) {
return result;
}
});
//push promise into array
promises.push(promise);
}
//when each promise in the promises array is complete
$.when.apply($, promises).then(function() {
console.log(promises);
});
}
});
This seems to be working, but to better organize this code, I want to put all of this code into an object and then abstract out the subsequent ajax call into it's own method. My code looks like this:
var myObject = {
firstRequest: function() {
$.ajax({
url: 'http://api.com',
success: function(result) {
var promises = [];
for(var i = 0; i < result.length; i++) {
var promise = myObject.secondRequest(result.param);
promises.push(promise);
}
$.when.apply($, promises).then(function() {
myObject.displayContent(promises));
});
}
});
},
secondRequest: function(paramVal) {
$.ajax({
url: 'http://api.com&param='+result.paramVal,
success: function(result) {
return result;
}
});
},
displayContent: function(promises) {
console.log(promises);
}
};
In this example, after running myObject.displayContent, each array item in promises is undefined. I think it's because myObject.secondRequest isn't actually a promise itself (the ajax request inside this function is the actual promise). How can I make this arrangement work?
UPDATE
Adding return to my ajax promise works but I am unable to access responseJSON in the ajax objects that are returned.
secondRequest: function(paramVal) {
return $.ajax({
url: 'http://api.com&param='+result.paramVal,
success: function(result) {
return result;
}
});
},
Here is an image of one of the objects that is returned from my $.when.then() callback. I am able to access the readyState property, but not the responseJSON property.
You have to pass a function to .then, but you are immediately executing myObject.displayContent. It should be:
$.when.apply($, promises).then(function() {
myObject.displayContent(promises)
});
If you don't actually need access to the promises itself, pass the function directly:
$.when.apply($, promises).then(myObject.displayContent);
// or with `$.proxy` if you need `this` to refer to the object
$.when.apply($, promises).then($.proxy(myObject, 'displayContent'));
You also have to return the promise from your secondRequest function:
secondRequest: function(paramVal) {
return $.ajax(...);
}
Otherwise promise will be undefined in
var promise = myObject.secondRequest(result.param);

Categories

Resources