+1 vote
in NodeJS Essentials by

What is an error-first callback?

1 Answer

0 votes
by
Error-first callbacks are used to pass errors and data as well. You have to pass the error as the first parameter, and it has to be checked to see if something went wrong. Additional arguments are used to pass data.

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

  if (err) {

    // handle the error, the return is important here

    // so execution stops here

    return console.log(err)

  }

  // use the data object

  console.log(data)

})

Related questions

+1 vote
asked May 31, 2020 in NodeJS Essentials by Robindeniel
+1 vote
asked May 31, 2020 in NodeJS Essentials by Robindeniel
...