gpt4 book ai didi

node.js - node.js 介绍 - 从 3 个 url (http.get) 打印数据

转载 作者:搜寻专家 更新时间:2023-11-01 00:41:00 25 4
gpt4 key购买 nike

我正在使用 learnyounode 介绍 node.js。我想知道您是否可以帮助实现这一点:异步。

问题来了:

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.

这是我的糟糕解决方案,实际上它不起作用。

var http = require('http');
var message = [];

for (var i = 2; i < 5; i++)
http.get(process.argv[i], function (res) {
res.setEncoding('utf8');

res.on('data', function(line) {
message[i] += line.toString();
});
res.on('end', function(line) {
for (var i = 0; i < 3; i++)
console.log(message[i]);
});
});

更新因此,我对您的解决方案尝试了类似的方法。

这里是:

var http = require('http');
var count = 0;
var message = ["","",""];

for (var i = 2; i < 5; i++)
{
http.get(process.argv[i], function (res) {
res.setEncoding('utf8');

res.on('data', function( line ) {
message[count] += line.toString();
});

res.on('end', function(line) {
count++;
if(count !== 3)
return;
else
printOutput();
});
});
}

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

但输出滞后:/(顺序不对)

  1. CURRENT: "He has not got the skite and watch out for the bogged Trent from punchy blue with the dry to the Vinnie's It'll be flanno where flat out like the slabs..."
  2. EXPECTED: "He's got a massive coldie my watch out for the smoko We're jackaroo going on she'll be right servo dramas.."
  3. CURRENT ". He has not got a banana bender piece of piss the dry as a budgie smugglers Come a flamin clacker you little bog standard ripper The cross them to his blood's worth bottling flamin the cunning of a rip snorter.."
  4. EXPECTED: "He has not got the skite and watch out for the bogged Trent from punchy blue with the dry to the Vinnie's It'll be flanno where flat out like the slabs..."
  5. CURRENT: "He's got a massive coldie my watch out for the smoko We're jackaroo going on she'll be right servo dramas.."
  6. EXPECTED: "He has not got a banana bender piece of piss the dry as a budgie smugglers Come a flamin clacker you little bog standard ripper The cross them to his blood's worth bottling flamin the cunning of a rip snorter..."
  7. CURRENT: ""
  8. EXPECTED ""

最佳答案

一种更简洁的异步方式是将所有 Promise 放在一个数组中,然后在该数组上调用 Promise.all()

var http = require('http');

promises = [
promiseLoad(process.argv[2]),
promiseLoad(process.argv[3]),
promiseLoad(process.argv[4])
];
Promise.all(promises).then(function(res){
console.log(res);
});

function promiseLoad(url) {
var body = '';
return new Promise(function(resolve, reject) {
http.get(url, function(res) {
res.on('data', function(d) {
body += d;
});
res.on('end', function() {
resolve(body);
});
});
});
}

关于node.js - node.js 介绍 - 从 3 个 url (http.get) 打印数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34546735/

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