2017-05-08 4 views
1

私はevalAsyncとを使用しているメソッドを持っています。約束待ち時間が最大タイムアウトに達すると約束した約束待ち時間をに設定したいと約束します解決され、空に戻されました。どのようにこれを達成するための任意のアイデアですか?

evalasyncとtimeoutという用語で検索するたびにソリューションを見つけることは難しく、結果を$ timeoutで "汚染"します。

if (angular.isFunction(this.search)) { 
     this.cancellation = this.$q.defer<void>(); 

     this.$scope.$evalAsync(() => { 
      const searchArgs= { 
       value: this.value, 
       cancellation: this.cancellation.promise 
      }; 

      const result = this.search(args); 
     }); 
    } 
}; 

おかげ

+2

あなたのコードを記入してください。 – Karim

+0

新しいダイジェストサイクルを防ぐために 'setTimeout'(' $ timeout'ではなく、 'this。$ scope。$ evalAsync'、 –

+0

の外側で' this._cancellation'を拒否してください)@AlonEitanあなたはその提案の例を挙げることができますか? – Javiere

答えて

2

docsで説明したように - "を$evalAsyncexpressionが実行されるときに保証しません"。あなたはXミリ秒後に約束を解決したい場合は(私たちはダイジェスト・サイクルをトリガにしたくないので、ない$timeout

だから、あなたはsetTimeoutとそれを組み合わせることができます。

あなたの問題を正しく理解しているかどうかわからないため、私はあなたに基本的なロジックを示しますので、私の答えを変更したり、役に立たない場合は削除してください:

var deferred = $q.defer(); 

// Save the timeout ID so we can know what to do with our promise 
var timeout = setTimeout(function() { 
    // Clear the timeout ID and reject the promise after 3 seconds 
    timeout = null; 
    deferred.reject(); 
}, 3000); 

$scope.$evalAsync(function() { 
    if(!timeout) { 
     // Too late, the promise was rejected inside 'setTimeout' 
     return; 
    } 

    // Prevent 'setTimeout' from being executed by clearing its ID 
    clearTimeout(timeout); 

    deferred.resolve({busy: false}); // Of course, you can pass any other value here 
}); 

deferred.promise.then(
    function(data) { console.log('resolved'); }, 
    function() { console.log('rejected'); } 
); 
関連する問題