nodeschool.io 9
~~ 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的更多相关文章
- nodeschool.io 4
~~ MY FIRST ASYNC I/O! ~~ Write a program that uses a single asynchronous filesystem operationto rea ...
- nodeschool.io 3
~~ MY FIRST I/O! ~~ Write a program that uses a single synchronous filesystem operation toread a fil ...
- nodeschool.io 2
~~ BABY STEPS ~~ Write a program that accepts one or more numbers as command-line arguments and pr ...
- nodeschool.io 10
~~ TIME SERVER ~~ Write a TCP time server! Your server should listen to TCP connections on port 8000 ...
- nodeschool.io 8
~~ HTTP COLLECT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the ...
- nodeschool.io 7
~~ HTTP CLIENT ~~ Write a program that performs an HTTP GET request to a URL provided toyou as the f ...
- nodeschool.io 6
~~ MAKE IT MODULAR ~~ This problem is the same as the previous but introduces the concept ofmodules. ...
- nodeschool.io 5
~~ FILTERED LS ~~ Create a program that prints a list of files in a given directory,filtered by the ...
- NODESCHOOL
来源:https://nodeschool.io/zh-cn/ 核心基础课程(Core) javascripting 学习 JavaScript 语言的基础,无需任何编程经验 npm install ...
随机推荐
- Hadoop分布式部署——要点
这里只记录几个要点,比较容易出问题的地方. 1.各服务器必须有相同的用户(便于使用相同的用户ssh登录)2.ssh互通,配置无密码登录ssh-keygen -t rsa,将id_rsa.pub的内容相 ...
- 【转】The decoupling capacitor…is it really necessary?
Before working as an applications engineer, I worked as an IC test development engineer here at TI. ...
- OGNL调用静态方法和属性
ognl的全名是 Object-Graph Navigation Language 表示的是图对象导航语言...我觉得它最厉害的一点是,通过"."来实现对象的导航...下面看他他的 ...
- SQL语句最基本的性能优化方法
有些人还不知道sql语句的基本性能优化方法,在此我简单提醒一下,最基本的优化方法: 1.检查是否缺少索引.调试的时候开启“包括实际的执行计划” 执行后会显示缺少的索引, 然后让dba帮助添 ...
- C#正则表达式编程(四):正则表达式
正则表达式提供了功能强大.灵活而又高效的方法来处理文本.正则表达式的全面模式匹配表示法使您可以快速分析大量文本以找到特定的字符模式:提取.编辑.替换或删除文本子字符串:或将提取的字符串添加到集合以生成 ...
- cocos2d-x 3.X (二)创建动起来的精灵
[参考文章]http://www.cnblogs.com/suguoqiang/archive/2013/04/03/2997316.html 在HelloWorldScene.h中声明void ro ...
- 纯css3样式属性制作各种图形图标
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- poj3449Geometric Shapes
链接 繁琐. 处理出来所有的线段,再判断相交. 对于正方形的已知对角顶点求剩余两顶点 (列出4个方程求解) p[].x=(p[].x+p[].x+p[].y-p[].y)/; p[].y=(p[].y ...
- mysql 锁的粒度
1.锁的类型分为读锁和写锁,这个很好区分.可以这样认为:如果有增删改,就是写锁.如果是查询,就是读锁.2.锁的粒度也就是锁的范围,分为行锁和表锁.锁的范围和多个因素有关,包括事务隔离级别.是否使用索引 ...
- JavaWeb 7 Servlet
7 Servlet Servlet学习的大纲:1. servlet概念及相关接口简介2. servet 执行过程3. servlet路径映射4. 缺省servlet --应用5. s ...