I am trying to send sms on loop with Cordova plugin.
The issue that big part of SMS not sent.
Dose cordova have some limitation or should I do some ideal time?
this my code:
var contactsLen = $scope.contacts.length;
for (var i = 0; i < contactsLen; i++) {
if ($scope.contacts[i].hasOwnProperty('number')) {
$cordovaSms
.send($scope.contacts[i].number, text)
.then(function () {
if (i == contactsLen - 1) {
$scope.log += 'send All!'
}
}, function (error) {
The plugin code that sends the SMS is asynchronous, which means running it in a loop like that will not work the way you expect. If you want to fire multiple async events and wait for them all to finish, then you need to use something like q$ (https://docs.angularjs.org/api/ng/service/$q) to handle it. Make note of the all() method which lets you pass an array of promises.
Related
Use Meteor and AngularJS, I have the following setup:
ctrl.js
for(var i = 0; i < result.length; i++){
Meteor.call('serverMethod', arg1, arg2, function(err, res){
console.log(res);
});
}
methods.js
'serverMethod' (arg1, arg2) {
return HTTP.call("GET", "http://example.com/foo/var");
}
With the aim being that, for each element in result, it would call the Meteor method on the server, which would make a HTTP GET request, returning some data and then logging that data.
This all works, however it is doing it one at a time (make request, wait for result, make next request). I thought this would all run asynchronously?
What have I missed here/ what can I improve?
You need to use this.unblock(); inside your meteor method here is a page for more info.
$scope.placeOrder = function() {
var callbackBalance = apiService.updateBalance($scope.order);
callbackBalance.then(function(data) {
if(data.data.success) {
var callback = apiService.createOrder($scope.order);
callback.then(function(data){
if(data.data.success)
{
localStorageService.cookie.clearAll();
alert("Order Placed Successfully");
$state.go("createOrder");
}
else
alert('Sorry! Cannot place order');
});
}
else
{
alert('Cannot update company balance')
}
});
This a code to place order for a company and update its balance amount according to the order total.The code works fine but according to this code first the balance amount API is called and once its response is success order API will be called and its response will be checked.But, how can we check if both are successful then only update databases for balance and order.Right now a case can be that balance is updated for a company but for some reason no order was placed.
I am using MEAN stack for my development.
You can use $q service from angular
A service that helps you run functions asynchronously, and use their
return values (or exceptions) when they are done processing. You can
create a request object like this
You can create request array and pass it to $a.all function.
var request = [apiService.updateBalance($scope.order), apiService.createOrder($scope.order)]
and use $q.all function to get provided request simultaneously
$q.all(request).then(function (response) {
}, function (error) {
});
that will get the request data simultaneously. Make sure to add $q as dependency.
I am making lots of API calls in my applications i.e say 50.
The total time for completing all the api calls will be around 1 minute. The priority for all the api calls will 2. I have enabled the angular cache.
So in meantime if the user of my applications just want to focus on the some of the api calls among the all i.e say just 6 api calls.
Then once again I will project that 6 api calls with priority 1 .
But still I dont get what I aimed ? i.e these 6 api calls need to receive the data asap.
Kindly refer the below example code .
On Initial load :
for(var i=1,priority=19;i<=19,priority>=1;i++,priority--)
{
$http.get("http://localhost:65291/WebService1.asmx/HelloWorld"+i+"?test=hari",{priority:2})
.then(function(response) { });
}
}
On some event click :
$http.get("http://localhost:65291/WebService1.asmx/HelloWorld7?test=hari",{priority:1})
.then(function(response) { });
}
if you want send multiple http request one shot then use $q.all
Inside the loop push the http requests to an array and send that http array at once.
var httpArr = []
for (var i = 1, priority = 19; i <= 19, priority >= 1; i++, priority--) {
httpArr.push($http.get("http://localhost:65291/WebService1.asmx/HelloWorld" + i + "?test=hari", {
priority: 2
}))
}
$q.all(httpArr).then(function(response) {
console.log(response[0].data) //1st request response
console.log(response[1].data) //2nd request response
console.log(response[2].data) //3rd request response
})
I'm trying to make a call to a function 'submittoServer' which is inside factory 'pService', which makes $http call and then broadcast the data. The call to the 'submittoserver' is happening inside a for loop. Problem here is that I couldn't see the actual call is being made until the last loop, which is sending only the last item, but as you see in the code below I want to update one particular variable after every call, can someone please suggest how can I don't that. I can't do call back here as I've other method that call this same factory function with different inputs.
for (var i = vr.lines.length - 1; i >= 0; i--) {
if (parseInt(vr.lines[i].id) === id && Boolean(vr.lines[i].IsVoided) != true) {
lineId = vr.lines[i].lineID;
pService.submitToServer(actionId, { "IData": id }, ineId)
linesRemoved = linesRemoved + 1;
}
if (linesRemoved === lineqty)
{
updateModel = true;
}
}
The problem here is that your service is a promise to return data. Your function will keep looping and running before the promise is resolved. You need to refactor your loop to take this into account.
Either add a .then(fn(){}) to handle the resolve promise. Gather up all the changed lineIds and submit them all at once and (again) handle the resolved promise with a .then(fn(){}). Lastly, given you next set of code logic, you probably want to do something more like $q.all to wait on all promise(s) to resolve before moving forward (see Wait for all promises to resolve)
Example
before your for loop:
var self=this;
self.linesRemoved = 0; // or init as needed.
Inside your for loop.
pService.submitToServer(actionId,{data}).then(function(resp){
self.linesRemoved++; // shortcut, this does +1 to itself.
});
Why do you have update model? With Angular your data is two-way bound and should just react to it being changed.
Sample $http call with return in a service, this is a promise itself:
return $http.post(url, data, { cache: true });
Use this in a controller like
service.callHttp(data).success(function(resp){
self.linesRemoved++;
}).error(function(resp){});
If you have to wait it might be better to hold all and wait until they are all finished.
var promises =[];
for(){
promises.push(service.callHttp());
}
$q.all(promises).then(function(){
// do work if(self.linesRemoved==lineQty)
// update... You can't evaluate until they are all finished right?
});
I am running an Angular app with a Parse backend. When I make a query, the app freezes, even when the DOM doesn't depend on the data being grabbing at the time. That is why this answer was not so helpful. Is there any way to have the rest of the app run along while some request is in progress?
Below is the code that freezes everything until the request resolves. The Parse Javascript SDK comes with promises, which is what 'QS.errorQuery.find()` returns.
function get() {
var queries = [
QS.errorQuery.find(),
QS.resLogQuery.find()
];
return $q.all(queries)
.then(function(res) {
return {
errors: res[0],
logs: res[1]
}; //end return
}); //end return
}; //end get
get().then(function(res) {
$scope.lineData = DP.bigLineParser(res.errors, res.logs);
});
The request is async so as the response is coming in, your UI wont freeze up. The long delay is probably happening when Parse gets its response from the server and parses it.