~~ 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

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

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. 在包a中新建一个类A,在类A中有一个int add(int m)方法,用来求1+2+…+m 的和。在包b中新建一个类B,在类B中有一个int cheng(int n)方法,用来求n! 的结果。在包c中新建一个主类C,调用A、B中的方法输出1+2+…+30的和, 以及5!的计算结果。

    package a; public class A { public void add(int m) { int sum=0; for (int i = 1; i <=m; i++) { sum ...

  2. 瞧,这就是UE4 C++

    1.虚幻中的类前缀你会见到U,A,F,以下就是很好的罗列其中的意义 U: UObject继承过来的,例如UTexture A: AActor继承过来的,例如AGameMode F: 其他的类和结构,例 ...

  3. poj 1654 Area (多边形求面积)

    链接:http://poj.org/problem?id=1654 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:  ...

  4. XAML基础

    1.标记扩展 将一个对象的属性值依赖在其他其他对象的某个属性上 用法:标记属性的一般用法是:Attribute = Value,使用标记拓展,Value字符串是由一对花括号及其括起来的内容组成,XAM ...

  5. LTE Module User Documentation(翻译2)——配置LTE MAC 调度器

    LTE用户文档 (如有不当的地方,欢迎指正!) 5 配置 LTE MAC 调度器   这里有几种 LTE MAC 调度器用户可以选择.使用下面的代码定义调度器的类型: Ptr<LteHelper ...

  6. [转载] zookeeper 事件通知

    ZK事件回调当一个client访问ZK时,client与ZK保持长连接.应用可以通过client的api注册一些callback,当对应的事件发生时,client会执行对应的callback.如果你基 ...

  7. nodejs学习笔记<五>npm使用

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题. 以下是几种常见使用场景: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从NPM服务器下载并 ...

  8. bootstrap学习笔记<七>(图标,图像)

    图像 bootstrap为图像预加载提供了很简洁的样式.(CDN:http://placehold.it/140x140:) PS:该CDN链接后的140x140可以根据网站需要更换合适的尺寸.例如: ...

  9. MTK Camera 开机启动流程(转载)

    一.MTK平台Camera框架 MTK平台的Camera的架构见下图, 这里主要介绍kernel部分和HAL层部分. 1.Kernel 部分主要有两块: 1.1.image sensordriver, ...

  10. sscanf的用法(转)

    队长做上海邀请赛的I题时遇到一个棘手的问题,字符串的处理很麻烦,按传统的gets全部读入的话还要做N多处理,太浪费时间. 回来之后搜了一下sscanf的用法发现可以很好的解决这一类问题,各种百度,转来 ...