gpt4 book ai didi

javascript - LearnYouNode - 杂耍异步

转载 作者:搜寻专家 更新时间:2023-10-31 23:41:53 25 4
gpt4 key购买 nike

我读了一些相同的教程问题,但我仍然无法理解。

我用下面的代码做对了

<code>
var httpModule = require('http');
var blModule = require('bl');
var arguments = process.argv;
var urls = [ arguments[2], arguments[3], arguments[4] ];
var results = [];
var count =0;
urls.forEach(
function(url , i)
{
var printResponse = function (response)
{

response.pipe(
blModule(
function(err, data)
{
count++;
if(err)
return console.log(err);

results[i] = data.toString();
//if(results.length===3)
if(count===3)
{
for( var c=0; c < results.length; c++)
console.log(results[c]);
}

}
)
);

}

httpModule.get(url, printResponse);

}
);

</code>

我不明白的部分是if(count===3) ,我知道通过检查 count == 3,我可以确定回调函数都被调用了。最初我使用 if(results.length === 3) 因为我认为这意味着我现在有所有结果,但它失败了,谁能解释一下?

谢谢

最佳答案

这只是一个 JS 的东西..

j = [] // j.length = 0
j[2] = 0 // now the j.length = 3 *magic*

据我所知,您的第三个请求是最快的 :P

关于javascript - LearnYouNode - 杂耍异步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33220903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com