Node Process Manager

Error First Callback

Nodejs

Free Online Tutorials on JavaScript, Node.js, React.js, PHP, JAVA, Python, HTML, GraphQl, TypeScript, MEAN, MERN Stack, etc.

Error first callback - Node.js

The first argument of the callback is reserved for an error object.
  1. 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. Second argument of the callback is reserved for any successful response data. If no error occurred, err will be set to null and data will be returned in the second argument.
fs.readFile('my_text_file.txt', function(err, data) {
	// If an error occurred, handle it (throw error)
	if (err) {
		console.log('Error Found:' + err);
		throw err;
	}
	// Otherwise, log the data
	console.log(data);
});
In Node.js, error convention, this kind of callback implementations are called error-first callback.