當下劃線完成時,是否有回調,因爲如果我console log
之後馬上顯然我填充每個循環的數組不可用。這是來自嵌套的_.each
循環。下劃線_each回調完成後?
_.each(data.recipe, function(recipeItem) {
var recipeMap = that.get('recipeMap');
recipeMap[recipeItem.id] = { id: recipeItem.id, quantity: recipeItem.quantity };
});
console.log(that.get('recipeMap')); //not ready yet.
'_.each'是同步的。它不需要回調。循環將在你的'console.log'行到達之前完成。如果你在'_.each'內部調用異步函數*,那就是另一個問題。 – meagar
'_.each'是同步的,並且只會在函數對所有項目執行後纔會返回。如果'that.get'是異步的,'each'不會幫助你。 – Bergi