1.

What do you mean by Error-First Callback?

Answer»

The “error-first” callback  is also known as an “errorback”, “errback”, or “node-style callback”. Error-first callbacks are utilized to pass ERRORS and data too. You need to pass the error as the main parameter, and you MUST verify whether something went wrong. Additional arguments are utilized to pass data.  There are two rules for defining an error-first callback: 

  1. The first argument of the callback is reserved for an error object.If an error occurred, it will be returned by the first err argument. 
  2. The second argument of the callback is reserved for any SUCCESSFUL response data. If no error occurred, err will be set to null and any successful data will be returned in the second argument. 

Code example is as below:- 

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)  })


Discussion

No Comment Found