Javascript 承诺链。等待最后的承诺解决

Javascript 承诺链。等待最后的承诺解决,javascript,angularjs,promise,Javascript,Angularjs,Promise,我有两个功能: getDocument: function(title){ var defer = $q.defer(); $timeout(function(){ defer.resolve(true); console.log(1); },2000); return defer.promise; }, anotherFunc : function(){ var defer =

我有两个功能:

    getDocument: function(title){

    var defer = $q.defer();

    $timeout(function(){

        defer.resolve(true);            
        console.log(1);

    },2000);

    return defer.promise;
},

anotherFunc : function(){

    var defer = $q.defer();

    console.log(2);
    defer.resolve(document);

    return defer.promise;

}
还有一个电话:

    when('/entry/:title', {templateUrl: 'partials/views/entry.php', controller: 'entryCtrl',resolve: {

    document: function($q,$route,$log,document){

        var defer = $q.defer();

        document.getDocument()
            .then(document.anotherFunc());               

        return defer.promise;
    }
}}).
尽管我已经对
getDocument()
应用了超时,但是调用了另一个func()get,即使承诺尚未解决

为什么会这样

我怎样才能避免这种行为

调用另一个函数func()get,即使承诺尚未解决

因为你把它叫做:

相反,您希望将一个函数传递到
then()
,该函数将在承诺解析时被调用:

….then(document.anotherFunc)

// or, more explicit and preserving 'this':
….then(function(promiseResult) {
    document.anotherFunc();
})
调用另一个函数func()get,即使承诺尚未解决

因为你把它叫做:

相反,您希望将一个函数传递到
then()
,该函数将在承诺解析时被调用:

….then(document.anotherFunc)

// or, more explicit and preserving 'this':
….then(function(promiseResult) {
    document.anotherFunc();
})

您有一些输入错误,document.getDocument()。然后(document.anotherFunc);您实际上是在代码中调用document.anotherFunc,而不是将其作为参数传递给then函数。@TestersGonnaTest谢谢,修复了。这不是真正代码的一部分,只是由于对某些输入错误进行了重构而产生的问题,document.getDocument().then(document.anotherFunc);您实际上是在代码中调用document.anotherFunc,而不是将其作为参数传递给then函数。@TestersGonnaTest谢谢,修复了。这并不是真正代码的一部分,只是由于SO的重构而产生的一个问题