javascript - API calls in angularJS right after each other -
i have problem using angular in ionic mobile project , have piece of code working great in desktop environment. 3 api call web server 500ms after each other. in phone using lower connection 1 of call fails whole process fail. there way api calls in exact moment previous 1 finish? mean, not using fixed amount of time.
//get member information $timeout(function() { membersservice.getmember(community.url, community.user.api_key, community.user.auth_token, $stateparams.userid). success(function(data) { $scope.member = data.result; }); }, 500); $timeout(function() { messagesservice.getconversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateparams.userid, "from"). success(function(data) { if(data.result["entities"].length > 0) { messages = messages.concat(data.result["entities"]); subject = "re: "+data.result["entities"][data.result["entities"].length-1].title; } $scope.messagedata.subject = subject; }); }, 1000); $timeout(function() { messagesservice.getconversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateparams.userid, "to"). success(function(data) { log = []; if(data.result["entities"].length > 0) { angular.foreach(data.result["entities"], function(v, k) { v.owner_guid = $stateparams.userid; log.push(v); }, log); messages = messages.concat(log); } var log = []; var count = 0; angular.foreach(messages, function(v, k) { messages_reorder.push(v); }, log); }); }, 1500);
this result of implementing nesting promise chains:
var loadmemberinfo = function( userid ) { return membersservice .getmember(community.url, community.user.api_key, community.user.auth_token, $stateparams.userid) .then(function(data) { $scope.member = data.result; }); }, getconversationfrom = function() { return messagesservice .getconversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateparams.userid, "from") .then(function(cf) { if(cf.data.result["entities"].length > 0) { messages = messages.concat(cf.data.result["entities"]); subject = "re: "+cf.data.result["entities"][cf.data.result["entities"].length-1].title; } $scope.messagedata.subject = subject; }); }, getconversationto = function() { messagesservice .getconversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateparams.userid, "to") .then(function(ct) { log = []; if(ct.data.result["entities"].length > 0) { angular.foreach(ct.data.result["entities"], function(v, k) { v.owner_guid = $stateparams.userid; log.push(v); }, log); messages = messages.concat(log); } //order array messages = messages.sort(function(a,b) { return a.time_created - b.time_created } ); var log = []; var count = 0; angular.foreach(messages, function(v, k) { messages_reorder.push(v); }, log); }); }, orderfullconversation = function() { $ionicloading.hide(); console.log(messages); if(messages_reorder.length > 5) { var messages_partial = messages_reorder.slice(messages_reorder.length-5,messages_reorder.length); } else { var messages_partial = messages_reorder; } $scope.messages = messages_partial; $scope.community = community; }; loadmemberinfo( $stateparams.userid ) .then( getconversationfrom ) .then( getconversationto ) .then( orderfullconversation );
more details here
Comments
Post a Comment