0

How to rewrite this code, to get the desired o/p. I would like to use the AgentReply object after filling in the data. Inside the switch case, this object has data. But once outside, it is null again. Understood that it is because of the async, But what should I do, to be able to use 'AgentReply' once it has data.

$scope.ActionItems = function (actionItem) {
var AgentReply = {};
switch (actionItem) {
 case "SendOTP":
                var SentStatus = "";                
                DataFactory.SendOTP('39487539847')
                .then(function (response) {
                    SentStatus = JSON.parse(JSON.parse(response.data));
                    SendOTPFailed();                        
                }, function (error) {
                });                
                break;
}/*End of switch*/
function SendOTPFailed(){
  if (SentStatus == "200") {
    AgentReply = {
      IsCustomer: false,
      UserText: "Request Failed.",
     }                            
  }
}

if (Object.keys(AgentReply).length > 0) {
   //do something with AgentReply  
  }
}

2 Answers 2

1

Just pass a function in to where the AgentReply is available, and define it underneath, ie:

$scope.ActionItems = function (actionItem) {
    var AgentReply = {};

    switch (actionItem) {
        case "SendOTP":

            var SentStatus = "";                
            DataFactory.SendOTP('39487539847')
                .then(function (response) {
                    SentStatus = JSON.parse(JSON.parse(response.data));
                    if (SentStatus == "200") {
                        AgentReply = {
                            IsCustomer: false,
                            UserText: "Request Failed.",
                        }                            
                    }
                    doSomethingWithAgentReply(AgentReply);
                }, function (error) {
            });                
            break;
    }
    console.log(AgentReply); //null here

    function doSomethingWithAgentReply(reply) {

        if (Object.keys(reply).length > 0) {
            //do something with AgentReply  
        }
    }
 }
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. Is there any other way, I can do it within $scope.ActionItems itself
@Qwerty Is there a reason why you'd need to?
@Qwerty It is in the $scope.ActionItems now, if that's what you meant?
I followed ur way, but the same prob. The Object.keys(reply).length.. is a common fn. for many other switch cases. Updated the Question. Please have a look
@Qwerty Are you sure SentStatus equals 200? And are you sure you are supposed to be using JSON.parse twice on the response.data?
|
1

If you need to use this code :

if (Object.keys(AgentReply).length > 0) {
   //do something with AgentReply  
  }
}

Outside the .then() function :

DataFactory.SendOTP('39487539847')
  .then(function (response) {

  })

You can try this:

$scope.ActionItems = function (actionItem) {

var def = jQuery.Deferred();

var AgentReply = {};
switch (actionItem) {
  case "SendOTP":
    var SentStatus = "";                
    DataFactory.SendOTP('39487539847')
      .then(function (response) {
        SentStatus = JSON.parse(JSON.parse(response.data));
        if (SentStatus == "200") {
          AgentReply = {
            IsCustomer: false,
            UserText: "Request Failed.",
          }                            
          def.resolve(AgentReply);
        }

        console.log(AgentReply); //available here
     }, function (error) {
        def.reject(error);
   });

   return def.promise();

   break;
}


//console.log(AgentReply); //null here
//if (Object.keys(AgentReply).length > 0) {
   //do something with AgentReply  
//  }
//}
// This is unusable in this case.

The usage is:

var someActionItem = 'SomeActionItemInfo';

$scope.ActionItems(someActionItem)
  .then(function(agentReply) {
    if (Object.keys(agentReply).length > 0) {
      //do something with agentReply
    }
  }, function(error));

EDIT:

$scope.ActionItems is the same function. What happening when you using promise?

First you defining the deffer object. var def = jQuery.Deferred(). This object is at jQuery, but all frameworks/libraryies that support promise working at the same way.

As you see, you returning def.promise(). That is the object which contain .then property. Because of that obj you can use $scope.ActionItems().then() method. That actually make def.promise().

And inside your async code (this code that consuming some time and it's not executed immediately) you defining def.resolve() or def.reject().

When the work is done. You calling def.resolve(withSomeData) and this will activate .then() method to the $scope.ActionItems.

For example:

var foo = null;

function doPromise() {
  var def = jQuery.Deferred();

  setTimeout(function(){
    foo = 2;

    def.resolve(foo + 1) // This will call the .then() method with foo + 1

  }, 500);

  return def.promise();
}

doPromise();
console.log(foo) // foo = null here. cuz the function waiting 500ms.

// Here the .then() method will be executed after ~500+ ms.
doPromise().then(function(fooValue) {
  console.log(fooValue) // foo value = 3 here. cuz function is done
});

1 Comment

is $scope.ActionItems(someActionItem) another function. I am unable to understand what happens after def.promise();break; from your code please.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.