Underscore

编程入门 行业动态 更新时间:2024-10-23 09:36:15
本文介绍了Underscore _.each回调完成后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当下划线完成时是否有回调它是 _。每个循环,因为如果我控制台日志之后立即显然我用每个循环填充的数组不可用。这是来自嵌套的 _。每个循环。

Is there a callback for when underscore is finished it's _.each loop because if I console log immediately afterwards obviously the array I am populating with the each loop is not available. This is from a nested _.each loop.

_.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.

推荐答案

每个 函数是同步的,完成时不需要回调。其中一个在循环之后立即执行命令将执行。

The each function in UnderscoreJS is synchronous which wouldn't require a callback when it is finished. One it's done executing the commands immediately following the loop will execute.

如果您在循环中执行异步操作,我建议使用支持异步操作的库。每个功能。一种可能性是使用 AsyncJS 。

If you are performing async operations in your loop, I would recommend using a library that supports async operations within the each function. One possibility is by using AsyncJS.

这是你的循环转换为AsyncJS:

Here is your loop translated to AsyncJS:

async.each(data.recipe, function(recipeItem, callback) { var recipeMap = that.get('recipeMap'); recipeMap[recipeItem.id] = { id: recipeItem.id, quantity: recipeItem.quantity }; callback(); // show that no errors happened }, function(err) { if(err) { console.log("There was an error" + err); } else { console.log("Loop is done"); } });

更多推荐

Underscore

本文发布于:2023-11-25 04:05:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Underscore

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!