# Node.js Asynchronous request assignment help

**URL:** <https://forum.freecodecamp.org/t/node-js-asynchronous-request-assignment-help/18873>\
**Category:** General\
**Created:** [July 9, 2016, 10:27pm UTC](https://forum.freecodecamp.org/t/node-js-asynchronous-request-assignment-help/18873 "2016-07-09T22:27:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicknyr](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nicknyr](https://forum.freecodecamp.org/u/Nicknyr)\
**Post date:** [July 9, 2016, 10:27pm UTC](https://forum.freecodecamp.org/t/node-js-asynchronous-request-assignment-help/18873/1 "2016-07-09T22:27:20Z")

</div>

I’m working on the Start a Node Server assignment and I’m stuck on the MY FIRST ASYNC I/O! (Exercise 4 of 13) assignment.

I’ve read a bunch of stuff about making async i/o requests and it seems very simple but I can’t get it to work. The instructions are ambiguous and it’s frustrating.

Here’s my code:

var fs = require(‘fs’);

fs.readFile(‘process.argv[2]’, ‘utf8’, function(err, data){  
if(err){  
console.error(err);  
}else  
console.log(data.split(’\n’).length - 1);

});

What’s the issue?

---

<div class="post-metadata">

**Author:** ![Nicknyr](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nicknyr](https://forum.freecodecamp.org/u/Nicknyr)\
**Post date:** [July 9, 2016, 11:28pm UTC](https://forum.freecodecamp.org/t/node-js-asynchronous-request-assignment-help/18873/3 "2016-07-09T23:28:37Z")

</div>

> [@Nicknyr](#):
>
> fs.readFile(‘process.argv[2]’, ‘utf8’, function(err, data){

I thought that took care of the callback function? I thought all I needed for that was the function(err, data) like in the synchronous assignment.

Your answer works with the exception that I had to add an if statement to take care of error handling. Thanks for the help. I have some reading to do on callbacks so I can fully understand this.

Here’s the official answer they give after you complete the assignment in case you’re curious.

var fs = require(‘fs’)  
var file = process.argv[2]

```
 fs.readFile(file, function (err, contents) {  
   // fs.readFile(file, 'utf8', callback) can also be used  
   var lines = contents.toString().split('\n').length - 1  
   console.log(lines)  
 })

```

---

<div class="post-metadata">

**Author:** ![batjko](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/batjko/32/4115_2.png) [@batjko](https://forum.freecodecamp.org/u/batjko)\
**Post date:** [July 10, 2016, 10:05pm UTC](https://forum.freecodecamp.org/t/node-js-asynchronous-request-assignment-help/18873/4 "2016-07-10T22:05:55Z")

</div>

Just a quick thought: You’re passing in `'process.argv[2]'`, i.e. as a string (with the quotes). I assume that was a typo?  
If not, that certainly would have prevented you from opening your file,as it would look for a file with the name “process.argv[2]”.

Other than that, your code looks like it should give you the number of lines just fine.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 1:09pm UTC](https://forum.freecodecamp.org/t/node-js-asynchronous-request-assignment-help/18873/6 "2021-01-16T13:09:59Z")

</div>

Closed on January 16, 2021, 1:09pm UTC


