javascript - learnyounode #9 杂耍异步

标签 javascript node.js

我正在尝试学习 nodeschool 的 learnyounode。

This problem is the same as the previous problem (HTTP COLLECT) in that you need to use http.get(). However, this time you will be provided with three URLs as the first three command-line arguments.

You must collect the complete content provided to you by each of the URLs and print it to the console (stdout). You don't need to print out the length, just the data as a String; one line per URL. The catch is that you must print them out in the same order as the URLs are provided to you as command-line arguments.

我对为什么我的解决方案不能正常工作感到困惑,因为它对我来说看起来一样,但功能更强大,而且我不确定他们的内部测试工作原理:

1.  ACTUAL:    ""
1.  EXPECTED:  "As busy as a dead horse also lets get some dero. Built like a sleepout no dramas lets get some chook. She'll be right thingo my she'll be right ute. "

2.  ACTUAL:    "She'll be right bizzo no worries she'll be right fair dinkum. We're going aerial pingpong no worries as busy as a gyno. "
2.  EXPECTED:  "She'll be right bizzo no worries she'll be right fair dinkum. We're going aerial pingpong no worries as busy as a gyno. "

3.  ACTUAL:    "He's got a massive pretty spiffy heaps she'll be right brizzie. He hasn't got a fly wire where shazza got us some strewth. She'll be right spit the dummy with it'll be fair go. We're going gobsmacked with as stands out like arvo. He's got a massive bush bash mate she'll be right slacker. "
3.  EXPECTED:  "He's got a massive pretty spiffy heaps she'll be right brizzie. He hasn't got a fly wire where shazza got us some strewth. She'll be right spit the dummy with it'll be fair go. We're going gobsmacked with as stands out like arvo. He's got a massive bush bash mate she'll be right slacker. "

4.  ACTUAL:    ""
4.  EXPECTED:  ""

我的代码:

var http = require('http');
var bl = require('bl');

var result = [];
var urls = process.argv.slice(2);
urls.forEach(function(url, i) {
  http.get(url, function(response) {
    response.pipe(bl(function(err, data) {
      if (err) return console.error(err);
      result[i] = data.toString();
      if (i === urls.length - 1) {
        console.log(result.join('\n'));
      }
    }));
  });
});

官方解决方案:

var http = require('http')
var bl = require('bl')
var results = []
var count = 0

function printResults () {
  for (var i = 0; i < 3; i++)
    console.log(results[i])
}

function httpGet (index) {
  http.get(process.argv[2 + index], function (response) {
    response.pipe(bl(function (err, data) {
      if (err)
        return console.error(err)

      results[index] = data.toString()
      count++

      if (count == 3)
        printResults()
    }))
  })
}

for (var i = 0; i < 3; i++)
  httpGet(i)

基本上第一个测试永远不会通过(尽管如果迭代数组中只有 1 个 url(而不是 3 个),第一个测试通过但其他测试不通过)。任何见解都会很棒。我不确定在哪里问这个问题,也许我只是遗漏了一些 JS 的东西,如果这不合适,我很抱歉。

最佳答案

您尚未确定是否已下载所有网址。

请求不一定按顺序返回。考虑3是否先回来。您将跳过其他两个 url,只打印出 3 个。

演示代码计算响应的数量,因此可以保证在打印出答案之前得到所有信息。

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

相关文章:

javascript - 使用 HTML 中的按钮访问和添加字段

javascript - Knockout js - 如果某些条件不匹配,如何将下拉值更改为以前的值

javascript - 如何使用 superagent 发送 FormData 对象

node.js - Node EADDRINUSE 错误 - 一个真正的解决方案?

c++ - 命令行工具输出后如何改变其输出?

javascript - angular-cli.json 在添加 style.css 时创建错误

javascript - 如何为可重用的 React 表组件分离出 JSON 对象

node.js - 我无法全局安装 nodemon, "nodemon"无法识别

node.js - SocketIO 发射不会在快速路线内触发

javascript - 按多个匹配值过滤数组