~~ MY FIRST ASYNC I/O! ~~

Write a program that uses a single asynchronous filesystem operation
to read a file and print the number of newlines it contains to the
console (stdout), similar to running `cat file | wc -l`.

The full path to the file to read will be provided as the first
command-line argument.

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

The solution to this problem is almost the same as the previous
problem except you must now do it the Node.js way: asynchronous.

Instead of `fs.readFileSync()` you will want to use `fs.readFile()`
and instead of using the return value of this method you need to
collect the value from a callback function that you pass in as the
second argument.

Remember that idiomatic Node.js callbacks normally have the signature:

function (err, data) { ... }

so you can check if an error occurred by checking whether the first
argument is truthy. If there is no error, you should have your
`Buffer` object as the second argument. As with `readFileSync()`,
you can supply 'utf8' as the second argument and put the callback as
the third argument and you will get a `String` instead of a `Buffer`.

Documentation on the `fs` module can be found by pointing your browser
here:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\fs.h
tml

var fs = require('fs');
fs.readFile(process.argv[2],"utf-8",function(err,data){
if(err) throw err;
console.log(data.split("\n").length-1);
});

nodeschool.io 4的更多相关文章

  1. nodeschool.io 3

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

  2. nodeschool.io 2

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

  3. nodeschool.io 10

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

  4. nodeschool.io 9

    ~~ JUGGLING ASYNC ~~ 其实就是一个循环,在循环里面输出的顺序,和排列后在外面的顺序不一样,这是为什么呢? 用第三方async包,直接报错了…… This problem is th ...

  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. DirectX 3d 取景变换

    在世界坐标系中,几何体和摄像机都是相对于世界坐标系定义的.但是当摄像机的位置和朝向任意时,投影变换及其它类型的变的就略显困难或效率不高.为了简化运算,我们将摄像机变的至世界坐标系原点,并将其旋转,使摄 ...

  2. 【转载】.NET程序员走向高端必读书单汇总

    原文:.NET程序员走向高端必读书单汇总 .NET程序员走向高端必读书单汇总 一.知识树 1. 基本能力 1.1 数学 1.2 英语 1.3 语言表达 2. 计算机组织与体系结构 3. 算法与数据结构 ...

  3. Spring整合Tiles

    1.假设Spring相关的包和配置已经导入成功(后续有时间补上,本项目用的是3.2.0版本). 2.导入Tiles相关的jar包. tiles-api-2.2.2.jar tiles-core-2.2 ...

  4. MySQL(三) —— 约束以及修改数据表

    约束: 1. 约束保证数据的完整性和一致性: 2. 约束分为表级约束和列级约束: 3. 约束类型包括:NOT NULL, PRIMARY KEY, UNIQUE KEY, DEFAULT, FOREI ...

  5. [c/c++]linux下使用c/c++操作mysql

    首先需要安装相应的库文件,直接apt-get就可以. sudo apt-get install libmysqlclient-dev 编译的时候,需要额外链接到这个库.如果是apt-get安装的话,那 ...

  6. [HDOJ1232]畅通工程

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. Maven初学

    先上几张霸气的图:

  8. [SAP ABAP开发技术总结]ABAP常用事务码

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. MemSQL Start[c]UP 2.0 - Round 1(无聊练手B题)

    http://codeforces.com/contest/452/problem/B   B. 4-point polyline time limit per test 2 seconds memo ...

  10. STORM_0009_Lifecycle-of-a-topology/拓扑的生命周期

    http://storm.apache.org/releases/1.0.1/Lifecycle-of-a-topology.html STORM拓扑的生命周期   本页的内容基于0.7.1代码,后来 ...