Dean Christian Armada Dean Christian Armada - 1 year ago 62
AngularJS Question

Why is the function inside my promise not running

I've heard that $q and promise is great for synchronous programming.

I want my second function to run after my first function which has a timeout. So basically I want mt first function to finish running first before my secondfunction operates

My code is:

<link rel="stylesheet" type="text/css" href="">
<script src=""></script>
<script src=""></script>

<body ng-app="myApp">
<div ng-controller="myCtrl">
<button ng-click="myClick()">Click Me!</button>
<script type="text/javascript">
angular.module('myApp', [])
.controller('myCtrl',['$scope', '$timeout', '$q', function($scope, $timeout, $q){
$scope.functionOne = function(){
return $q(function(resolve, reject){
}, 3000);
$scope.functionTwo = function(){

$scope.myClick = function(){
var promise = $scope.functionOne();
}, function(){


Answer Source

The promise is created but never resolved. But you don't need it at all! $timeout already returns a promise:

$scope.functionOne = function(){
  return $timeout(function(){
  }, 3000);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download