- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用 async.whilst 重新生成一个介于 0 和数组长度之间的随机数,直到该索引上的元素长度大于指定长度。我想为此使用 async.whilst,但语法对我来说并不完全清楚。我考虑过执行以下操作:
var selectParagraph = function(paragraphs, callback){
var index = Math.floor(Math.random() * paragraphs.length
async.whilst(
function(){
return paragraphs[index].length < minParagraphLength;
},
function(cb) {
index = Math.floor(Math.random() * paragraphs.length);
},
function(err) {
console.log(paragraphs[index]);
callback(err, paragraphs[index]);
}
}
但是,这是行不通的。我想这是因为我没有在任何地方将 cb 用于第二个功能,但我不完全知道我应该如何使用它。更改索引后我只调用 cb() 吗?变量err到底包含什么?
最佳答案
I suppose it is because I didn't use the callback for the second function anywhere
是的,没错。 async.js
希望您在完成后回调,如果您不这样做,它将不会继续进行下一次迭代。
but I don't exactly know how I should use it
你根本不应该使用它,因为你没有做任何异步的事情。使用标准的 do while
循环:
do {
var index = Math.floor(Math.random() * paragraphs.length);
} while (paragraphs[index].length < minParagraphLength)
console.log(paragraphs[index]);
callback(null, paragraphs[index]); // not sure where you're getting `callback` from
关于javascript - async.whilst 中的 callback 和 err 是做什么用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29969647/
这个问题在这里已经有了答案: 关闭 10 年前。
正如问题所述,async.whilst 只会调用一次。但是,如果我传递一个包含 2 个对象(长度为 2)的数组,它只会调用一次——而不是为数组的每个索引调用一次。 //if previous awsK
var count = 0; async.whilst( function () { return count < 5; }, function (callback) {
我只是尝试使用 async.whilst(),如 here 所示. 这是我的简单代码,取自他们的文档: var async = require('async'); console.log('start
我有以下表格: 槽 = id, time child = id, parent_id, name, form parent = id, name, contact_details 老师 = id, n
我想运行一个 while 循环,该循环取决于每次迭代更新的条件。现在的挑战是每次迭代中的代码都是异步的。 实现此目的的一种方法是使用 async.whilst()。但是,我正在尝试使用 Bluebir
我在 async.whilst 中使用 async.waterfall,在 async.series 中像这样(只崩溃一次,但随后继续应该如此): function(callback){ async
我是 Docker 的新手,目前正在关注本教程: Learn Docker in 12 minutes 我创建了必要的文件,并使其显示“Hello World!”在本地主机上:80。 除此之外,我尝试
我有以下脚本: public void addcheese(int addcheese) { if (cheeseamount + addcheese 1900) && (cheeseamo
我收到此错误!缺少 ) 即使我的 jquery 行似乎没有任何问题。 $('.product_area').append(' " />
我想在选择输入字段时显示一个元素,但最好不要使用任何 JavaScript 来执行此操作。 因为我不能将元素放在输入字段中(很明显),当输入字段处于事件状态时,我不能在我的 CSS 中定位它(我假设
我正在尝试开发一种算法来测试二进制数 A 是否是二进制数 B 的“子数”。 如果仅使用 B 的“1”位就可以创建 A,则 A 是 B 的子数。 例如: 如果 B = 十进制 5 = 二进制 101然后
我正在尝试使用 async.whilst 重新生成一个介于 0 和数组长度之间的随机数,直到该索引上的元素长度大于指定长度。我想为此使用 async.whilst,但语法对我来说并不完全清楚。我考虑过
尝试在 ubuntu 9.10 64 位上安装 psycopg2 时出现错误。 错误是: >>> import psycopg2 Traceback (most recent call last):
我有一个非常复杂的表单,它提供了运行脚本(我们自己的类型)的选项。当它运行时,我不想完全锁定 UI,所以我想在一个线程中启动它。到目前为止一切顺利,但为了防止用户弄乱我需要有选择地禁用部分 UI 的东
我正在开发一个Android应用程序,我需要保存需要通过输入日期来区分的数据。 为此,我想到了以下方法: 为每个日期创建一个新表并将数据存储在其中。因此,该表将仅包含 2 个属性,即 item_num
我正在为游戏中的玩家生成一些随机的 x 和 y 坐标: var xcoordRed = Math.floor((Math.random() * 790) +1); var ycoordRed = Ma
这里以各种复杂的形式提出了类似的问题。一些使用 jQuery: How to toggle animate with css display:none . 还有一些非常具体的内容忽略了要实现的目标的范
我正在努力实现一些我不确定是否可能实现的事情。请注意,我正在寻找纯 CSS 解决方案,我知道我可以用 JS 解决这个问题,但我不想这样做。 考虑以下笔: https://codepen.io/fchr
问题 一旦用户在播放媒体期间尝试与 Google Home 设备交互,是否可以恢复媒体对象的内容? 问题 假设您已经开始使用 conv.ask 播放 mp3 文件。您对 conv.ask 的调用将类似
我是一名优秀的程序员,十分优秀!