The file read could be async, but your code fileReadCallBack will never be
called.

After fs.readFile(...) all the async magic take place, OK.

BUT YOUR JAVASCRIPT code will run the next command, after fs.readFile. It
is an infinite loop, becase fileReadCallBack will be called when your
JavaScript code free the JavaScript machine, so it can attend any callback

If you don't free the JavaScript machine, fileReadCallBack will never
executed, and your buffer will be empty for ever

Maybe better is

fs.readFile(process.argv[2], fileReadCallBack);

// Our callback function for our asyncronous file reading
function fileReadCallBack(err, data) {
  if(err)
      return; // or maybe better, inform the problem
var numOfNewLines = data.toString().split('\n').length-1;
console.log(numOfNewLines);

} }

Apologize my English ;-)

Angel "Java" Lopez
@ajlopez


On Sat, Jan 24, 2015 at 8:25 PM, Maverick Peppers <[email protected]>
wrote:

> // Start of program4 process
> var fs = require('fs');
>
> // Asyncronously read and send the buffer object of a file whose path
> // is set int 2nd argument of the process to the callback function.
> var buffer = 0;
> fs.readFile(process.argv[2], fileReadCallBack);
>
> while(buffer == 0) {}
>
> var numOfNewLines = buffer.toString().split('\n').length-1;
> console.log(numOfNewLines);
>
> // Our callback function for our asyncronous file reading
> function fileReadCallBack(err, data) { if(!err){ buffer = data; } }
>
> I'm using NodeSchool.io's workshop <http://nodeschool.io/#workshoppers>
> to learn Nodejs. I'm on problem 4, *Async File I/O *and the above code
> runs forever. If the file reading is asynchronous, I'm thinking *var
> buffer* should no longer be 0 and the while-loop will terminate and move
> on.
>
> I know the correct solution would place the logic inside of the callback
> function, but I'm curious as to why the code above doesn't work as expected.
>
> Thanks!
>
> --
> Job board: http://jobs.nodejs.org/
> New group rules:
> https://gist.github.com/othiym23/9886289#file-moderation-policy-md
> Old group rules:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> ---
> You received this message because you are subscribed to the Google Groups
> "nodejs" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/nodejs/635a38d4-4f7c-402c-b841-7b682814143e%40googlegroups.com
> <https://groups.google.com/d/msgid/nodejs/635a38d4-4f7c-402c-b841-7b682814143e%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/CAMs%2BDqJp9NUWvWtTjbPECQczyxZikM941_%2BUwjdgGcNWWLtzLw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to