Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!


Unexpected sync method: '{a}'

When do I get this error?

JSLint will throw the "Unexpected sync method: '{a}'" error when it encounters an attempt to access a property whose identifier ends with the string "Sync". In the following example we attempt to get an array of file names within a directory (this is a Node.js example):

Note that although the error messages states "method", JSLint will actually raise this error for an attempt to access any property that fits the criteria, whether it's a method or not (it doesn't check to see if a pair of invoking parentheses follow the identifier).

Why do I get this error?

This error is raised to highlight a lack of convention and possible bad practice. Your code should run without problems if you don't change it, but there are likely to be better ways to acheive the same result.

In Node.js in particular, there are many asynchronous methods that provide synchronous equivalents. For example, there is a readdir method that is the asynchronous version of the readdirSync method in our example above.

In almost all situations it's preferrable to use the asynchronous method over the synchronous one. The reason is that synchronous methods will block execution until they have finished doing whatever they need to do. In the browser this can result in an apparently "frozen" page. To fix this issue, simply rework your code to use the asynchronous version of the method:

Alternatively, if you have a real need to use synchronous methods, you can surpress this error by setting the stupid option to true. You can tell by the name of the option how Douglas Crockford, author of JSLint, feels about this:


James Allardice

This article was written by James Allardice, an enthusiastic young JavaScript developer at Global Personals (we're looking for developers so please apply). He is passionate about writing clean, maintainable JavaScript and uses JSHint every day to help achieve this.

You can follow him on Twitter, fork him on GitHub and find him on Google+.


Proceeds generated by this site are donated to help advance other open source projects



comments powered by Disqus