~~ 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. 【leetcode❤python】121. Best Time to Buy and Sell Stock

    #-*- coding: UTF-8 -*- #Method1 :超时#class Solution(object):#    def maxProfit(self, prices):#      # ...

  2. UVA 10817 十一 Headmaster's Headache

    Headmaster's Headache Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Sub ...

  3. 【转】Kali Linux 新手折腾笔记

    原作者:http://defcon.cn/1618.html 最近在折腾Kali Linux 顺便做一简单整理,至于安装就不再多扯了,估计会出现的问题上一篇文章<VMware虚拟机安装Kali ...

  4. SQL疑难杂症【3】链接服务器提示"无法启动分布式事物"

    今天接到用户反馈,应用系统出现异常,无法正常使用,于是用Profiler跟踪了一下语句,发现执行的存储过程中调用了链接服务器,做了一些跨服务器操作数据的动作,刚好就是这个链接服务器出错了,错误截图如下 ...

  5. 【VB6笔记-02】从Command中获取链接参数

    Public Sub GetParameters() Dim Para As String Para = Command$() gstrUserID = GetCommandPara(Para, ) ...

  6. Protractor AngularJS测试框架教程

    Protractor是一个建立在WebDriverJS基础上的端到端(E2E)的AngularJS JavaScript Web应用程序测试框架.Protractor全自动化真实的模拟用户在真正的浏览 ...

  7. JavaScript中的加法运算

    <head runat="server"> <title>JavaScript实现加法计算器</title> <script type=& ...

  8. perl的map函数

    perl的map函数的使用: 语法 map EXPR, LIST map BLOCK LIST 定义和使用 对list中的每个元素执行EXPR或BLOCK,返回新的list.对每一此迭代,$_中保存了 ...

  9. iOS - OC NSProcessInfo 系统进程信息

    前言 @interface NSProcessInfo : NSObject NSProcessInfo 类中包含一些方法,允许你设置或检索正在运行的应用程序(即进程)的各种类型的信息. 1.获取系统 ...

  10. iOS - Swift Array 数组

    前言 public struct Array<Element> : CollectionType, MutableCollectionType, _DestructorSafeContai ...