~~ JUGGLING ASYNC ~~

其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢?

用第三方async包,直接报错了……

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.

----------------------------------------------------------------------
HINTS:

Don't expect these three servers to play nicely! They are not going to
give you complete responses in the order you hope, so you can't
naively just print the output as you get it because they will be out
of order.

You will need to queue the results and keep track of how many of the
URLs have returned their entire contents. Only once you have them all,
you can print the data to the console.

Counting callbacks is one of the fundamental ways of managing async in
Node. Rather than doing it yourself, you may find it more convenient
to rely on a third-party library such as http://npm.im/async or
http://npm.im/after. But for this exercise, try and do it without any
external helper library.

----------------------------------------------------------------------

jugglingAsync.js

var bl = require("bl"),
http = require('http'),
count = 0,
result = []; function printSult(){
for(var i = 0 ; i < 3 ; i++){
console.log(result[i]);
}
} function httpGet(index){
http.get(process.argv[2 + index], function(res) {
res.pipe(bl(function(err,data){
if(err) console.log(err);
result[index] = data.toString();
count ++ ;
if(count == 3){
printSult();
}
}));
});
} for(var i = 0 ; i < 3 ; i++){
httpGet(i);
}

nodeschool.io 9的更多相关文章

  1. nodeschool.io 4

    ~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...

  2. nodeschool.io 3

    ~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...

  3. nodeschool.io 2

    ~~  BABY STEPS  ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...

  4. nodeschool.io 10

    ~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...

  5. nodeschool.io 8

    ~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...

  6. nodeschool.io 7

    ~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...

  7. nodeschool.io 6

    ~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...

  8. nodeschool.io 5

    ~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...

  9. NODESCHOOL

    来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...

随机推荐

  1. Statement和PreparedStatement批量更新

    优势:1.节省传递时间. 2.并发处理. PreparedStatement: 1) addBatch()将一组参数添加到PreparedStatement对象内部. 2) executeBatch( ...

  2. Android 内存溢出解决方案(OOM) 整理总结

    在最近做的工程中发现加载的图片太多或图片过大时经常出现OOM问题,找网上资料也提供了很多方法,但自己感觉有点乱,特此,今天在不同型号的三款安卓手机上做了测试,因为有效果也有结果,今天小马就做个详细的总 ...

  3. CodeForces 219C Color Stripe

    Color Stripe Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  4. Intellij IDEA中使用Struts2

    据说struts2中有很多的漏洞, 不过作为学习我也就用了吧, 因为书上面是按着这个讲的呀. 难怪官网上也没有struts2.2.1的版本的下载. 1. 下载struts2.2.1 ga版本 2. 新 ...

  5. C# 线程(三):如何操纵一个线程

    From : http://kb.cnblogs.com/page/42529/ 下面我们就动手来创建一个线程,使用Thread类创建线程时,只需提供线程入口即可.(线程入口使程序知道该让这个线程干什 ...

  6. 微信公众平台开发详细步骤与java代码

    1.微信公众平台设置 首先在https://mp.weixin.qq.com/注册一个公众平台账号(服务号.订阅号.企业号的区别) 微信公众平台地址:https://mp.weixin.qq.com ...

  7. [转载] C++ 程序员快过来围观:非常实用全面的 C++ 资源

    原文: http://codecloud.net/c-plus-plus-resource-2983.html 绝对是c++开发者的福音啊, 必须推荐. 这次的资源涉及到了标准库.Web应用框架.人工 ...

  8. 百度之星Astar2016 Round2A

    All X 等比数列求和一下 A/B MOD C = A MOD (B*C) / B  或者分治一下 Sitting in Line 状压+拓扑dp dp(i, j)表示当前二进制状态为j,当前状态的 ...

  9. default(T)的含义

    default(T)是泛型中初始化的用法.因为对于泛型T你不知道是值类型还是引用类型,所以传参数是可能会出错.这里就要用到default(T). T t=default(T),就是初始化,值类型的话, ...

  10. unique-substrings-in-wraparound-string(好)

    https://leetcode.com/problems/unique-substrings-in-wraparound-string/ 好,我自己做出来的.多总结规律,多思考. package c ...