-3

In nodeJs callback pattern, an error is encountered during execution. this error is passed as the first argument to the callback! What are the advantages of this pattern?

for example:

fs.readFile(filePath, function(err, data) { }

Why err is the first argument in this pattern?

mojtaba ramezani
  • 1,461
  • 16
  • 15

1 Answers1

2

More often you are more interested if an error occurred, than to the actual data returned, so you will write this more:

function(err) {}

if the error was not the first argument, you always have to use a variable for the data you don't care about.

function(_, err) {}

More over, the error first approach, will 'force' you, or at least remind you to check if there is an error, if you already have to name it in your callback

Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149