Node.js

Node.js filesystem: ReadFile vs ReadFileSync

ReadFile

fs.ReadFile() takes 3 arguments:

1. The first argument this function takes is "path"
Path takes in a string, integer or an URL for the filename or file descriptor.

2. The second argument this function takes is "options"
Options can take in a string to indicate a specific encoding of a file

3. The last argument it take is a "callback"
Callback takes in a function that is used for error handling or continue the program.

ReadFileSync

For this version of ReadFile it only takes  2 arguments:

The first two arguments are the same as fs.ReadFile() it takes the path and options.

Now the main question is when is it time to use one over the other?
For this question you would need to ask yourself "Do you need to do things at the same time or do you need the program to wait until you finish a part?"

If you needed to run the whole program then you would need to use the fs.ReadFile() function.
On the other hand the fs.ReadFileSync() function returns the contents of the file instead of continuing the program.

Comments

Popular posts from this blog

My third Hacktoberfest 2018 Journey

DPS909 release 0.3

Contributing to 2048 Repo cont'd