LearnYouNode练习“Juggling Async”可选解决方案

时间:2016-02-23 15:30:12

标签: javascript node.js asynchronous

在“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);
});

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

1 个答案:

答案 0 :(得分:2)

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

async

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中,回调是通知异步作业完成的唯一方法。这就是为什么在两个示例中调用提供的回调都很重要。