- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在努力解决 learnyounode
上的 JUGGLING ASYNC
任务。据我所知,我做的事情几乎是正确的,看起来问题出在异步的某个地方,而我自己试图避免bl
或其他模块。我在没有其他模块的情况下完成了上一个任务,并希望继续这种趋势。
const http = require('http');
const url = process.argv[2];
let content = [];
let count = 0;
const arguments = process.argv;
let urlArray = arguments.filter(function pullUrls(element, index, array) {
return index >= 2;
});
function printResults() {
for (var i = 0; i < 3; i++) {
console.log(content[i]);
}
}
function httpGet(index) {
http.get(urlArray[index], function(response){
response.on('data', function(data){
newData = data.toString();
content[index] = content[index] + newData;
})
response.on('end', function(){
count++;
if (count === 3){
printResults();
}
})
})
}
for (var i = 0; i < 3; i++) {
httpGet(i);
}
常见输出非常接近预期,但在开头包含一个 undefined
字符串,我不明白为什么。
undefinedLets get some ute when we're going parma. Grab us a slab how it'll be bull bar. He's got a massive bushie where stands out like a pot.
undefinedShe'll be right mongrel heaps as cross as a hit the turps. Stands out like a booze also you little ripper flick. As stands out like ironman when lets throw a bikkie.
undefinedHe hasn't got a bounce with gutful of struth. Stands out like a aerial pingpong piece of piss built like a battler.
正如您所看到的,它首先找到数组“未定义”,这是正确的,然后附加到它上面。
我最好的猜测是,content[index] = content[index] + newData;
行以某种方式保留了 let content = []
的未定义性质在计算出 content[i]
之前。现在我已经把它写出来并仔细考虑了,这可能是一个简单的 JS 问题,我只是忽略了或者不知道我不知道。
任何帮助都会很好。
最佳答案
您获得了正确的输出,content[index]
最初是未定义的,您可以在连接 newData
之前进行未定义的检查。这是经过更改的完整代码。
const http = require('http');
const url = process.argv[2];
let content = [];
let count = 0;
const arguments = process.argv;
let urlArray = arguments.filter(function pullUrls(element, index, array) {
return index >= 2;
});
function printResults() {
for (var i = 0; i < 3; i++) {
console.log(content[i]);
}
}
function httpGet(index) {
http.get(urlArray[index], function(response){
response.on('data', function(data){
let newData = data.toString();
//check if undefined
if(typeof content[index] !== 'undefined'){
content[index] = content[index] + newData;
}else{
//add newData if undefined
content[index] = newData;
}
})
response.on('end', function(){
count++;
if (count === 3){
printResults();
}
})
})
}
for (var i = 0; i < 3; i++) {
httpGet(i);
}
关于javascript - learnyounode 练习 9 - 输出显示未定义、理解异步或逻辑的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42899518/
我已经查看了有关此主题的其他线程,但仍然无法弄清楚为什么我的线程不起作用。有什么想法吗? var files = process.argv.slice(2); var count = 0; var r
我读了一些相同的教程问题,但我仍然无法理解。 我用下面的代码做对了 var httpModule = require('http'); var blModule = require('bl'); v
我一直在 learnyounode 的帮助下学习 Nodejs,但我陷入了杂耍异步。我的解决方案除了问题中最困难的部分外都有效 - 它打印的结果不按顺序排列,因为某些请求先于其他请求完成。我试图在不使
我正在尝试学习 nodeschool 的 learnyounode。 This problem is the same as the previous problem (HTTP COLLECT) i
我正在尝试学习 nodeschool 的 learnyounode。 This problem is the same as the previous problem (HTTP COLLECT) i
这里似乎有很多关于这个问题的问题,但没有一个与我的问题 AFAICT 直接相关。以下是问题陈述: This problem is the same as the previous problem (H
有很多方法可以编写此代码,但我不明白为什么我的版本不起作用。 该程序应从命令行获取 2 个参数。第一个是包含文件的文件夹的路径,第二个参数是文件扩展名。输出应仅打印与第二个参数具有相同扩展名的文件。当
我一直在努力解决 learnyounode 上的 JUGGLING ASYNC 任务。据我所知,我做的事情几乎是正确的,看起来问题出在异步的某个地方,而我自己试图避免bl或其他模块。我在没有其他模块的
所以我正在解决这个名为杂耍异步的learnyounode练习,它基本上给了我3个URL并要求我按照URL输入的顺序输出URL内容。 我想出了一个递归解决方案,其中一个回调调用另一个回调,同时维护退出计
在“LearnYouNode”练习中,我对任务 9 有以下问题。在提示中提到,此练习也可以使用“asycn”或“after”包来完成。我尝试了两者,但都失败了:|(我已经完成了推荐的解决方案,没有额外
我是 Node 的新手,我正在努力通过 nodeschool.io learnyounode 交互式类(class),我遇到了 http-collect 问题。 首先是使用第三方包的实际解决方案Buf
在learnyounode上做教程,遇到如下问题。来自描述: You will be provided with three URLs as the first three command-line
所以我现在正在学习node.js,并且之前做过一些多任务处理,我的概念是异步和多任务处理有很多相似的问题,这让我想到了我的问题。 官方针对这个问题的解决方案是: var http = requir
我正在通过 learnyounode 教程学习 node.js,但一直停留在 HTTP-COLLECT 练习中。我想让我的代码在没有第三方程序帮助的情况下工作。它几乎可以正常工作,但由于某种原因,它没
当我在第 14 行运行下面的 list[i] 时,返回未定义,我不知道为什么。特别是因为我的第 10 行日志按预期返回。下面是代码, 1 var fs = require('fs');
我正在尝试使用 OSX 终端中的“Learnyounode”来学习 Node.js。当我使用 'learnyounode run programfile.js programfile.js' 运行下面
我试图在 nodeschool.io 的“learnyounode”研讨会中解决“Juggling Async”问题。我在这里看到了很多关于这个问题的问题,它不起作用,因为 url 是从循环中调用的。
我不断收到以下错误: ✗ Error connecting to localhost:20496: connect ECONNREFUSED 注意:本地主机号总是在变化。 在 cloud9.ide 上
下面是nodeschool learnyounode模块的习题5 创建一个程序,打印给定目录中的文件列表,按文件扩展名过滤。您将获得一个目录名称作为您程序的第一个参数(例如 /path/to/dir/
这个程序让我很困惑。该程序的目标是计算文件中换行符的数量并在命令提示符下输出。 Learnyounode然后对文件运行他们自己的检查,看看他们的答案是否与您的答案相符。 所以我从答案开始: var f
我是一名优秀的程序员,十分优秀!