Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What's the difference between check synchronously and asynchronously in check?

What's the difference between check synchronously and asynchronously in check?

Problem

What's the Difference between check existing file synchronously and asynchronously in node.js?

For example:

var path = require('path');
if (path.existsSync("/the/path")) { // or fs.existsSync
    // ...
}

and

// Is it a directory?
lstat('/the/path', function(err, stats) {
    if (!err && stats.isDirectory()) {
        // Yes it is
    }
});
Problem courtesy of: Rahman

Solution

The synchronous versions of the fs method provide their results via the method's return value; as a result these methods have to block while the I/O is performed to determine the result.

The asynchronous versions provide their results via the method's callback function that the caller provides as a parameter to the method. The methods just initiate the required I/O and then return immediately, so the return value from these methods isn't useful. When the I/O later completes, the callback is invoked to provide the result back to the caller.

Solution courtesy of: JohnnyHK

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

What's the difference between check synchronously and asynchronously in check?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×