learnyounode#9杂耍异步

时间:2015-04-06 03:37:17

标签: javascript node.js

我正在尝试通过nodeschool的learnyounode。

  

此问题与上一个问题(HTTP COLLECT)相同   你需要使用http.get()。但是,这次你会   提供了三个URL作为前三个命令行参数。

     

您必须收集每个提供给您的完整内容   URL并将其打印到控制台(stdout)。你不需要打印出来   长度,只是作为字符串的数据;每个网址一行。问题是   您必须按照与URL相同的顺序打印出来   作为命令行参数提供给你。

我很困惑为什么我的解决方案不能完全正常工作,因为它看起来和我一样但功能更强,并且不确定它们的内部测试工作:

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的东西,如果这不合适,那就很抱歉。

6 个答案:

答案 0 :(得分:5)

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

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

演示代码会计算响应次数,以确保在打印出答案之前获取所有内容。

答案 1 :(得分:2)

我认为您只需要等到所有请求结果结束或任何一个错误。我的答案是:

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

var urls = process.argv.slice(2)
var count = urls.length;

var results = [];

urls.forEach((url, index) => {
    http.get(url, (res) => {
        res.pipe(bl((err, data) => {
            if (err) throw err;

            results[index] = data.toString();
            count--;

            if (count == 0) {
                results.forEach((result) => {
                    console.log(result)
                });
            }
        }))
    })
})

答案 2 :(得分:1)

var http = require('http');

var links = [2, 3, 4];

var buffer = [];

(function render(index) {

http.get(process.argv[links[index]], function (response){

    response.setEncoding('utf8');

    response.on('data', function(chunk){
        if(buffer[index] === undefined) {
            buffer[index] = '';
        }
        buffer[index] += chunk;
    });
    response.on('end', function () {
        var newIndex = index+1;
        if(links[newIndex] !== undefined) {
            render(newIndex);
        } else {
            return renderOutput();
        }
    });
    response.on('error', console.error);

}).on('error', console.error);
})(0); //self-calling function

function renderOutput() {

buffer.forEach(function (elem) {

console.log(elem);

});
}

答案 3 :(得分:1)

我在不使用bufferList(bl)模块的情况下工作,可能是更通用的方法。

        var http = require('http');
        var urlList = [];
        urlList.push(process.argv[2]);
        urlList.push(process.argv[3]);
        urlList.push(process.argv[4]);
        var results = []
        var count = 0

        function getURLdata (index) {
            http.get(urlList[index], function(response){
                var data = {};
                data[index] = '';      
                response.setEncoding('utf-8');
                response.on('error', function(err){
                    console.log(err);
                });
                response.on('data', function(chunk){
                    data[index] += chunk;
                });


                response.on('end', function(){
                    results[index] = data;
                    count++;
                    if (count == urlList.length){
                        for (var i = 0; i < urlList.length; i++){
                            console.log(results[i][i]);
                        } 
                    }
                });

            });
        }

        for (var i = 0; i < urlList.length; i++)
            getURLdata(i);

答案 4 :(得分:0)

我是初学者所以也许这个解决方案有问题,这是使用async / await,通过制作一个promises数组并等待它们解决,这将控制响应的顺序

const axios = require("axios")
const  getURL = async url =>
{
  let res = await axios.get(url)
  return res.data
}

const getUrlArray = () => 
{
  let args = process.argv.slice(2)
  .map(e => getURL(e))
  return Promise.all(args)
}

getUrlArray()
.then(data => data.forEach(e => console.log(e)))

答案 5 :(得分:0)

非常简单的解决方案,但可以完成工作:

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

for (x = 2; x < 5; x++) {
  http.get(process.argv[x], function (res) {
    res.pipe(bl(function (err, data) {
      if (err) { return console.error(err) }
      console.log(data.toString());
    }));
  });
};