gpt4 book ai didi

javascript - LearnYouNode 练习 "Juggling Async"可选解决方案

转载 作者:行者123 更新时间:2023-12-02 14:58:59 25 4
gpt4 key购买 nike

在“LearnYouNode”练习中,我对任务 9 有以下问题。在提示中提到,此练习也可以使用“asycn”或“after”包来完成。我尝试了两者,但都失败了:|(我已经完成了推荐的解决方案,没有额外的包)

这是我的悲惨尝试:

两种变体的共享代码:

var http = require("http");
var bl = require("bl");
var after = require("after");
var async = require("async");
var results = [];

//getting the 3 arguments
var urls = []
for(var i = 2; i < process.argv.length; i++){
urls.push(process.argv[i]);
}
function printResults(){
for(var i = 0; i < results.length; i++){
console.log(results[i]);
}
}

“尝试后”:

//Understood it that way printResults is called after the var next is 3 times generated, no result at all
var next = after(3, printResults)
for(i = 0; i<urls.length;i++){
next(i);
}

var next = function (i){
http.get(urls[i], response => {
response.setEncoding('utf8');
var singleString = "";
response.on("data", data =>{
singleString += data;
}).on("end",function() {
results.push(singleString);
});

response.on("error", err => {
return console.log(err);
});

});
}

“asycn”尝试:

// I get output but in the wrong order
async.map(urls,function(url, printResults) {
http.get(url, response => {

response.setEncoding('utf8');
var singleString = "";
response.on("data", data =>{
singleString += data;
}).on("end",function() {
console.log(singleString);
});

response.on("error", err => {
console.log(err);
});

});
}, function(err){
console.log(err);
});

我真的不明白我做错了什么。非常感谢您的帮助。亲切的问候,桑德曼爵士

最佳答案

这都是关于语法的,你应该更仔细地阅读文档。

对于异步:

var http = require('http');
var urls = process.argv.slice(2);
var async = require('async');

// the second parameter is a callback provided by async
// it allows async to know when your async task is done
async.map(urls, (url, next) => {
http.get(url, response => {
var str = '';
response
.on('data', data => str += data)
.on('end', () => next(null, str)); // we use the callback here to give the data back
});
}, (err, results) => {
// the second callback is not an error handler
// it's called when all async jobs are done
// so the logging of the results goes here
results.forEach(res => console.log(res));
});

对于 after 解决方案,您声明了 next() 两次,并且您没有在 http.get()< 中使用原始的 next()/:

var http = require('http');
var urls = process.argv.slice(2);
var after = require('after');

var results = [];
function printResults() {
results.forEach(res => console.log(res));
}

// when you call next(), it tells after 'hey, my async job is done'
var next = after(3, printResults);

urls.forEach((url, i) => {
http.get(url, response => {
var str = '';
response
.on('data', data => str += data)
.on('end', () => {
// after is very "dumb", you have to store your result yourself at the right index
results[i] = str;
// this is very important, 'hey, my async job is done'
next();
});
});
});

在这两个解决方案中,您注意到都有一个回调(我在这两个解决方案中都将其称为 next),在异步 Javascript 中,回调是通知异步作业完成的唯一方法。这就是为什么在两个示例中调用提供的回调都很重要。

关于javascript - LearnYouNode 练习 "Juggling Async"可选解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35581566/

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