~~ 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. 【leetcode❤python】326. Power of Three

    #-*- coding: UTF-8 -*- class Solution(object):    def isPowerOfThree(self, n):        if n<=0:    ...

  2. TCP/IP协议简介

    计算机网络是什么? 简单地理解,计算机网络的任务就是传输数据.为了完成这一复杂的任务,国际标准化组织ISO提供了OSI参考模型,这种模型把互联网网络氛围7层,分别是物理层.数据链路层.网络层.传输层. ...

  3. DEV界面皮肤

    1.添加一个 2.添加引用: 3.添加一个SkinTools类 public class SkinTools { /// <summary> /// 在Program中调用 /// < ...

  4. XSS防御篇

    上周要求写自己用任何语言写一个留言版,存到数据库中,用自己的办法来解决XSS 我用了JSP+MYSQL,自己写了一个过滤器来防御WEB XSS漏洞 package com.mess.filter; p ...

  5. ARM工作模式

    ARM工作模式 学习ARM的最好的资料是ARM公司发布的资料:ARM Architecture Reference Manual.pdf 找到章节:Programmers’ Model 由文档可知:A ...

  6. UML分析与设计

    考点: 掌握面向对象的分析与设计 掌握UML描述方法 用例图.类图.序列图.状态转换图 类图:类的属性.方法的识别:类间的各种关系 类图:实体.联系 各种关系图例: 泛化:取公共属性 关联分为聚合.组 ...

  7. hdu 4946 Area of Mushroom(凸包)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4946 Area of Mushroom Time Limit: 2000/1000 MS (Java/Ot ...

  8. HDU 5810 Balls and Boxes(盒子与球)

     Balls and Boxes(盒子与球) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  9. XAF使用数据库访问层缓存的提升性能

    很多时候,为了提升性能,我们可以给数据库访问层做缓存. 以下几步可以完成这个任务:1. 使用自定义的 XPObjectSpaceProvider1.1. 创建自定义的 XPObjectSpacePro ...

  10. web服务器安装配置

    学习目标 javaweb概念和web资源分类 服务器的分类和常用服务器apache说明 tomcat 服务器目录结构介绍和工程发布 虚拟主机说明和配置 1.Web的概念 1.1.JavaWeb的概念 ...