Javascript $timeout包装$http.post返回未定义而不是承诺

Javascript $timeout包装$http.post返回未定义而不是承诺,javascript,angularjs,timeout,promise,angular-promise,Javascript,Angularjs,Timeout,Promise,Angular Promise,我试图提交一个表单,要求用户的电子邮件不重复,但我想在POST请求之前制作一个小动画。在$scope.process函数中,我得到: TypeError:无法读取未定义的属性“catch” 发生这种情况是因为$scope.process在$http.post完成之前返回,但是如何使process()返回承诺而不是未定义 这就是我到目前为止所做的: //The submit form function $scope.submitForm = function () { $('.btn').a

我试图提交一个表单,要求用户的电子邮件不重复,但我想在POST请求之前制作一个小动画。在
$scope.process
函数中,我得到:

TypeError:无法读取未定义的属性“catch”

发生这种情况是因为
$scope.process
$http.post
完成之前返回,但是如何使process()返回承诺而不是未定义

这就是我到目前为止所做的:

//The submit form function
$scope.submitForm = function () {

  $('.btn').attr('disable');

  if ($scope.form.$valid) {

    $scope.process($scope.account)
    .catch(function (err) {

      if (err.code === 'duplicate') {
        // handle error
      }

    });

    return false;
  }
};

//This is the one in charge to send the request
$scope.process = function(body) {

  // Timeout before http post to wait for animation
  $timeout(function() {

    return $http.post(postUrl, body).then(function (response) {
      // This return a promise if I remove the $timeout
      var nextPage = response.data;
    }).catch(function (err) {
      throw err;
    });
  }, 300);

  // Return undefined due to $timeout
};

提前感谢。

您得到了
TypeError:无法读取未定义的
的属性“catch”,因为您根本没有从
进程
函数返回承诺。 从
过程
函数和应用
返回
$timeout
承诺。然后
捕获
$timeout
承诺对象

通过返回
$timeout
服务,内部
$http.post
将返回一个数据,这样将形成适当的链接机制

代码

$scope.process = function(body) {
  // returned promise from here
  return $timeout(function() {
    //returned $http promise from here.
    return $http.post(postUrl, body).then(function (response) {
      // This return a promise if I remove the $timeout
      nextPage = response.data;
      return nextPage; //return data from here will return data from promise.
    }).catch(function (err) {
      throw err;
    });
  }, 300);
};

令人惊叹的!作品就像一个魔咒,不知道$timeout还回承诺,谢谢@汤姆萨杜很高兴知道。。谢谢:-)