Cyclotron3x3 Cyclotron3x3 - 1 year ago 38
AngularJS Question

defer.promise returning same result for simultaneous calls with different data inputs in angularJS

I have a factory which does a JavaScript remoting to my salesforce
apex class and return promises. I'm making call to a factory method
multiple times simultaneously which will return me defer.promise for
each call. But somehow its always returning me result of first call
every time. Even though my response from server is different inside
factory method during defer.resolve(response), when returning
defer.promise its returning the same result every time to controller.
I tried debugging but couldn't find any success. could anyone suggest
what am I doing wrong.

Here is my factory method:

app.factory('FieldSetFactory', ['$q', '$log', function($q, $log) {

var fieldSet = {};
var defer = $q.defer();

fieldSet.getFields = function(scope,fieldSetName, objectName) {
this.scope = scope;
var self = this;

FieldSetController.getFieldSetInfo(fieldSetName,objectName,function(response, event) {
if(event.status) {
console.log('response =',response); //this prints the actual result returned from server for different calls with different parameters
else {


return defer.promise;

return fieldSet;


My controller :
I have kept my contoller name as fieldSetJsController{!cid} where {!cid} is unique and passed by any component which is trying to use it.This is to make controller scope to individual calls and not overwriting same everytime, some other component passed some data.

app.controller('fieldSetJsController{!cid}', ['$scope', 'dataService',
'requestNotificationChannel','FieldSetFactory', function($scope,

requestNotificationChannel,FieldSetFactory) {

console.log("response in controller..............",response); //this response always shows reslut of first call and any subsequent calls return same defer.promise
$scope.fieldsetList = JSON.parse(response.replace(/&/g,'').replace(/quot;/g,'"'));
console.log("FiledSets..............",$scope.fieldsetList); //this is always printing same result for different calls with different parameter


These are my components which are calling same controller with different {!cid} value to differentiate them.

<c:FieldSetComponent objectAPIName="Applications__c"
fieldSet="Application_FieldSet_One" cid="Applications_FieldSet_One"
sectionTitle="Section 1" columns="2" textAlign="center">

<c:FieldSetComponent objectAPIName="Applications__c"
fieldSet="Application_FieldSet_Second" cid="Applications_FieldSet_Second"
sectionTitle="Section 2" columns="2" textAlign="center">
<c:FieldSetComponent objectAPIName="clcommon__Collateral__c"
fieldSet="Collateral_FieldSet_One" cid="Collateral_FieldSet_One"
sectionTitle="Section 3" columns="2" textAlign="center">

Answer Source

The problem is here:

var defer = $q.defer();

You're generating your deferred object in the Factory's constructor (instead of creating a new one every time the function is called), so your app is resolving the same promise object over and over.

Move var defer = $q.defer() to inside the factory method and it should work properly.