UNPKG

workshopper-browser-guide

Version:

Create an html browser version of the exercise descriptions

91 lines (85 loc) 4.51 kB
<!doctype html> <html class="no-js" lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>learnyounode Guide</title> <meta name="description" content="learn git and github"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="assets/css/style.css"> <link rel="stylesheet" href="assets/css/code.css"> <link href='assets/fonts/fonts.css' rel='stylesheet' type='text/css'> </head> <body> <!--[if lt IE 8]> <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> <![endif]--> <header class="site-header"> <div class="nav u-posFixed"> <ul class="nav-lang"> <li><a href="my_first_async_io.html" >English</a></li> <li><a href="my_first_async_io.es.html" >Español</a></li> <li><a href="my_first_async_io.ja.html" >日本語</a></li> <li><a href="my_first_async_io.pt-br.html" >Português (Brasil)</a></li> <li><a href="my_first_async_io.ru.html" >Русский</a></li> <li><a href="my_first_async_io.zh-cn.html" >中文 (中国)</a></li> <li><a href="my_first_async_io.zh-tw.html" >中文 (臺灣)</a></li> </ul> <div class="wrap-width u-textCenter"> <a href="my_first_io.html" <span class="u-floatLeft hand"></span> </a> <a class="filledblock" href="index.html">learnyounode</a> <a href="filtered_ls.html" <span class="u-floatRight hand"></span> </a> </div> </div> <div class="wrapper"> <div class="u-floatLeft"> <span class="all-caps">CHALLENGE</span> <h2 class="challenge-name">MY FIRST ASYNC I/O!</h2> </div> <div class="u-floatRight u-textRight"> <span class="all-caps">NUMBER</span> <h2 class="challenge-name">4 / 13</h2> </div> </div> </header> <div class="wrapper"> <p>Write a program that uses a single <strong>asynchronous</strong> filesystem operation to read a file and print the number of newlines it contains to the console (stdout), similar to running <code>cat file | wc -l</code>.</p> <p>The full path to the file to read will be provided as the first command-line argument.</p> <hr> <h1 id="hints">HINTS</h1> <p>The solution to this problem is <em>almost</em> the same as the previous problem except you must now do it <strong>the Node.js way</strong>: asynchronous.</p> <p>Instead of <code>fs.readFileSync()</code> you will want to use <code>fs.readFile()</code> 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. To learn more about callbacks, check out: <a href="https://github.com/maxogden/art-of-node#callbacks">https://github.com/maxogden/art-of-node#callbacks</a>.</p> <p>Remember that idiomatic Node.js callbacks normally have the signature:</p> <pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callback</span> <span class="hljs-params">(err, data)</span> </span>{ <span class="hljs-comment">/* ... */</span> } </code></pre> <p>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 <code>Buffer</code> object as the second argument. As with <code>readFileSync()</code>, you can supply &#39;utf8&#39; as the second argument and put the callback as the third argument and you will get a <code>String</code> instead of a <code>Buffer</code>.</p> <p>Documentation on the <code>fs</code> module can be found by pointing your browser here: <a href="../node_apidoc/fs.html">/node_apidoc/fs.html</a></p> <hr> <div class="prenext"> <div class="u-floatLeft"> <a href="my_first_io.html" class="u-inline-block all-caps">MY FIRST I/O! <div></div> </a> </div> <div class="u-textRight u-floatRight"> <a href="filtered_ls.html" class="u-inlineBlock all-caps">FILTERED LS <div></div> </a> </div> </div> <footer> <!-- <ul> <li class="all-caps"><a href="index.html"><strong>Challenges</strong></a></li> <li class="all-caps"> <a href="https://github.com/rvagg/learnyounode/issues/new" target="_blank">Open an Issue</a> </li> </ul> --> </footer> </div> </body> </html>