0
function load_file_contents(path, callback) {
    fs.open(path, 'r', function (err, f) {
        if (err) {
            callback(err);
            return;
        } else if (!f) {
            callback(make_error("invalid_handle",
                "bad file handle from fs.open"));
            return;
        }
        fs.fstat(f, function (err, stats) {
            if (err) {
                callback(err);
                return;
            }
            if (stats.isFile()) {
                var b = new Buffer(10000);
                fs.read(f, b, 0, 10000, null, function (err, br, buf) {
                    if (err) {
                        callback(err);
                        return;
                    }

                    fs.close(f, function (err) {
                        if (err) {
                            callback(err);
                            return;
                        }
                        callback(null, b.toString('utf8', 0, br));
                    });
                });
            } else {
                calback(make_error("not_file", "Can't load directory"));
                return;
            }
        });
    });
}


load_file_contents(
    "test.txt",
    function (err, contents) {
        if (err)
            console.log(err);
        else
            console.log(contents);
    }
);

In this code, I don't quite understand where does this "f" come from? after "fs.open()", there is a line

" } else if (!f) {"

what does this mean, where does this f comefrom?

4
  • fs.open calls callback with two arguments, first is error (or null, if there is no error) and second is file descriptor. Commented Jul 21, 2014 at 5:15
  • BTW, is it a awful implementation of fs.readFile? Commented Jul 21, 2014 at 5:15
  • 1
    I think you are very new to this concept, So you need to work on it for a little more days to come across. Study the node.js api and here is the fs.open api link nodejs.org/api/fs.html#fs_fs_open_path_flags_mode_callback In above f is file descriptor passed by the server which is developed by C++, C this is called as an v8 javascript engine Commented Jul 21, 2014 at 5:20
  • thanks @saikiran.vsk i saw this line "The callback gets two arguments (err, fd). " good . Commented Jul 21, 2014 at 5:24

1 Answer 1

1

f is the file descriptor that is passed to fs.open()'s callback if the file was able to be opened.

 else if (!f) {
     callback(make_error("invalid_handle",
         "bad file handle from fs.open"));
     return;
 }

is checking if the file descriptor is falsy (presumably it's checking if it's null or undefined) and calling the function's own callback with an error.

Sign up to request clarification or add additional context in comments.

1 Comment

Would you have an example as to what has to happen for both err and f to be falsy? I would assume that if err was falsy, then no error has happened and I would never think of checking f too...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.