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 ...
随机推荐
- Outlook 无法更新全球通讯簿,错误 0×80190194
当 Outlook 客户端尝试更新全球通讯簿,实际上是下载脱机通讯簿(Officeline Address Book,简称 OAB)时,可能会收到 0×80190194 的错误.错误代码 0×8019 ...
- 重定向redirect与跳转forward区别
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoResponse.a ...
- MySql中使用日期函数获取昨天的数据
.body-classic{ color:#444; font-family:Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Rom ...
- iOS开发学习笔记:基础篇
iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...
- JAVAWEB安全开发
晚上在看司马的博客,看到一篇关于JAVA安全的,基础的,蛮不错的,给大家分享下 文章来源是司马的博客:http://www.nxadmin.com/web/1332.html ============ ...
- [SAP ABAP开发技术总结]EXIT-COMMAND
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- CUBRID学习笔记 22 插入数据
CREATE TABLE auto_tbl(id INT AUTO_INCREMENT, name VARCHAR); 自增长的列可以插入null, 同时一次可以插入多条记录.别的和其他的sql数据库 ...
- 委托、匿名方法、Lambda表达式的演进
摘自:"http://www.cnblogs.com/eagle1986/archive/2012/01/19/2327358.html 假设给我们一个泛型对象List<T>,T ...
- ManualResetEvent & AutoResetEvent
参考资料: 1. https://msdn.microsoft.com/en-us/library/system.threading.manualresetevent.aspx 2. https: ...
- [转载] 深入 nginx 架构
原文: http://www.cnbeta.com/articles/402709.htm 了解 nginx 架构帮助我们学习如何开发高性能 web 服务. 为了更好地理解设计,你需要了解NGINX是 ...