2017-04-04 24 views
0

場景:recommendationsArr是一個項目數組(它的99.9%非空,但因爲它的外部API調用,我更喜歡檢查)。目的是爲valueOne,validItem和更新的recommendationsArr設定值。
有效性取決於valueOne的存在,所以如果recommendationsArr[0]有效valueOne那麼我不需要獲取數組其餘部分的結果。如何對異步承諾電話進行同步條件檢查

const getContent = function (item) { 
    console.log("####### Inside the getContent function #########"); 
    contentResponse = fetchContent(item); 
    return contentResponse; 
} 

if (recommendationsArr.length > 0) { 
    console.log("####### If Condition #########"); 
    recommendationsArr.find((item) => { 
    getContent(item).then(function(response){ 
     try { // to get valueOne 
      console.log("####### Try for: ######### ", term); 
      valueOne = response.content.valueOne; //may or may not be present 
      recommendationsArr.splice(recommendationsArr.indexOf(item), 1); // this is for styling first occurence is styled differently so thats popped out 
      validItem = item; 
      console.log("##### Valid item is: ", term); 
      return true; 
     } catch (err) { 
      console.log("##### Item not valid: ", recommendationsArr.indexOf(item)); 
      valueOne = null; 
      return false; 
     } 
    }); 
    }); 

    console.log("######### Just hanging out!!! #########"); 

    return { 
    component: <Layout><ComponentName 
     //pass other non-dependent props 
     validItem={validItem} 
     valueOne={valueOne} 
     recommendationsArr={recommendationsArr} 
    /></Layout>, 
    title: `Page Title`, 
    }; 
} 

return null; 

假設recommendationsArr = ["blue", "white", "green", "red"]; //usually the array is anywhere between 12-50 elements

這是怎麼回事,控制檯日誌:

####### If Condition ######### 
####### getApiContent response for ######### blue 
####### getApiContent response for ######### white 
####### getApiContent response for ######### green 
####### getApiContent response for ######### red 
######### Just hanging out!!! ######### 
####### Try for: ######### blue 
//I see data here 
##### Valid item is: blue 
####### Try for: ######### white 
//I see data here 
##### Valid item is: white 
####### Try for: ######### green 
//I see data here with valueOne missing in returned data 
##### Item not valid: green 
####### Try for: ######### red 
//I see data here 
##### Valid item is: red 

正如你看到的,API請求getContent保持請求所有的條款,然後奧菱到達.then。這導致了我不需要的一大堆api請求 - 響應,我知道我試圖在asyc .then上同步調用try/catch,但我不知道如何實現這一點。

理想情況下,不應調用API,除非.then返回false,如果try返回true - 不再有API請求並退出。另外,我需要訪問.then以外的response來更新組件的道具。我該如何實現這個目標?我簡要閱讀了關於async library,是否適合這種情況?

任何幫助/指導表示讚賞。我一直堅持這一

+0

那是什麼'recommendationsArr.find(...)'調用,爲什麼你不理它的結果呢? – Bergi

+0

@Bergi因爲我不想遍歷整個數組,所以對第一個最佳匹配感興趣,我正在使用arr.find(https://developer.mozilla.org/en-US/docs/Web/JavaScript/ Reference/Global_Objects/Array/find)而不是arr.forEach。我不確定我是如何忽略其結果的,你能否在代碼中指出? – user988544

+0

既不能用於異步回調: - /你只是沒有對返回值做任何事情而忽略結果。 – Bergi

回答

0

花一些時間與它擺弄左右之後,我想通了,所以最好不要嘗試和黑客和使用async-library

我現在用的是eachSeries方法來同時運行一個異步操作,

var asyncEachSeries = require('async/eachSeries'); 
.   
.   
. 
if (recommendationsArr.length > 0) { 
    asyncEachSeries(recommendationsArr, function (item, callback){ 
    getContent(item).then(function(response){ 
     try { 
      //do stuff 
      return true; 
     } catch (err) { 
      //do stuff 
     } 
     return response; 
    }); 
    }); 
}