1

I have 2 functions, say A() and B(). I want to call function B() after the function A() execution is completed. and I dont want to write the the function B() calling inside the definition of function A(), because function A() is used in other modules too and that take unnecessary. I am trying to call function A() first and then once it is done then call function B(). because function B() uses function A() $scope variable

I use .then() but its giving error .then() undefined something. I called function B() inside function A() that works but i don't want that way.

$scope.fetchApplicantData();  //function A()
$scope.fetchMobileMessages($scope.ApplicantMicroDetails.AD_I_PASTEMP_RESI_CONTACT_NO);    //function B()

I am expecting to call function A() first once it is done call the function B(). because function B() uses function A() variable, but is don't want to call function b() inside the definition in function A()

imran ali
  • 383
  • 1
  • 13
sayyed tabrez
  • 116
  • 1
  • 1
  • 11
  • possibke dupliacte of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – AZ_ Jul 26 '19 at 06:20
  • If the functions are asynchronous, they need to be coded so that they return promises. The `.then() undefined` message means that the functions are not returning promises and are not properly coded in a way that can be chained. To help you fix the functions we need to see the code for those functions. – georgeawg Jul 26 '19 at 07:32
  • ok thanks, let me fix this function first and try – sayyed tabrez Jul 26 '19 at 07:39

1 Answers1

2

You can use Promises like this:

var promise1 = new Promise(function(resolve, reject) {
  setTimeout(function() {
    resolve('foo');
  }, 300);
});

promise1.then(function(value) {
  console.log(value);
  // expected output: "foo"
});

console.log(promise1);
dganenco
  • 1,596
  • 1
  • 5
  • 16
alpi rawat
  • 53
  • 6