2016-09-28 163 views
0

我有一個服務設計,它需要一些參數,然後通過輸入數組循環,並找出哪個項目是空的,然後將它們添加到數組,然後$ q所有解決它應該給我的空物品陣列。需要幫助解決嵌套承諾,承諾沒有得到解決

輸入項目

function getElements(inputs) { 
      var elements= [], 
       promise, whenPromise, 
       promises = [], 
       mainPromise = $q.defer(); 
      if (inputs.length === 0) { 
       mainPromise.resolve(elements); 
       return mainPromise.promise; 
      } 

      angular.forEach(inputs, function (input) { 
       promise = getPromises(input); 
       whenPromise = $q.resolve(promise).then(function (response) { 
       $timeout(function() { 
         if (response.isEmpty) { 
        **//perform action with the response.data;** 
         } 
        }); 
       }, function() { 

       }); 

       promises.push(whenPromise); 
      }); 


      $q.all(promises).finally(function() { 
       mainPromise.resolve(outdatedEntities); 

      }); 

      return mainPromise.promise; 
     } 

     function getPromises(input) { 
      var deferred = $q.defer(); 

      someSerivce.getItemDetails(input.Id).then(function (value) { 
       if (value === null) { 
        $timeout(function() { 
         deferred.resolve({data: input, isEmpty: true}); 
        }); 

       } else { 
        //have item locally, but need to see if it's current 
        input.isEmpty().then(function (isEmpty) { 
         $timeout(function() { 
          deferred.resolve({data: input, isEmpty: isEmpty}); 
         }); 
        }, function (error) { 
         deferred.reject(error); 
        }); 
       } 

      }); 

      return deferred.promise; 
     } 

的陣列現在的問題是$ q.all是永遠不會解決。儘管所有內部承諾都已得到解決。

+0

是達到了這個代碼'如果(response.isEmpty){'每個承諾? –

+0

是的,但是在放置$ timout包裹之後放入 – Dreamweaver

+0

_之後放置$ timout_你是什麼意思? –

回答

1

這應該工作:

function getElements(inputs) { 
    var elements = [], 
      promise, whenPromise, 
      promises = [], 
      mainPromise = $q.defer(); 
    if (inputs.length === 0) { 
     mainPromise.resolve(elements); 
     return mainPromise.promise; 
    } 

    angular.forEach(inputs, function (input) { 
     promise = getPromises(input); 
     whenPromise = promise.then(function (response) { 

      if (response.isEmpty) { 
       * *//perform action with the response.data;** 
      } 

     }, function() { 

     }); 

     promises.push(whenPromise); 
    }); 


    return $q.all(promises).finally(function() { 
     return outdatedEntities; 
    }); 
} 

function getPromises(input) { 
    return someSerivce.getItemDetails(input.Id).then(function (value) { 
     if (value === null) { 
      return {data: input, isEmpty: true}; 
     } else { 
      //have item locally, but need to see if it's current 
      return input.isEmpty().then(function (isEmpty) { 
       return {data: input, isEmpty: isEmpty}; 
      }, function (error) { 
       return error; 
      }); 
     } 
    }); 
} 
+0

抱歉,但我有一個小的疑問: 返回deferred.resolve({數據:輸入,isEmpty:isEmpty});?在哪裏這個承諾諾言被定義 – Dreamweaver

+0

@Dreamweaver,是的,一個錯字,只是用'return {data:input,isEmpty :isEmpty}' –

+0

好吧:) ..我只是做了同樣的事情,但想與你檢查.. :) – Dreamweaver