Skip to content

Commit

Permalink
doc: change "Node.js style cb" to "error-first cb"
Browse files Browse the repository at this point in the history
Change the awkward "Node.js style callback" phrasing to the more
informative "error-first callback."

PR-URL: #17638
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Vse Mozhet Byt <[email protected]>
Reviewed-By: Jon Moss <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Gireesh Punathil <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
  • Loading branch information
ramsgoli authored and MylesBorins committed Jan 9, 2018
1 parent a93ed5c commit e059bc5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
21 changes: 11 additions & 10 deletions doc/api/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,35 +128,36 @@ they are thrown *after* the calling code has already exited.
Developers must refer to the documentation for each method to determine
exactly how errors raised by those methods are propagated.

### Node.js style callbacks
### Error-first callbacks

<!--type=misc-->

Most asynchronous methods exposed by the Node.js core API follow an idiomatic
pattern referred to as a "Node.js style callback". With this pattern, a
callback function is passed to the method as an argument. When the operation
either completes or an error is raised, the callback function is called with
the Error object (if any) passed as the first argument. If no error was raised,
the first argument will be passed as `null`.
pattern referred to as an _error-first callback_ (sometimes referred to as
a _Node.js style callback_). With this pattern, a callback function is passed
to the method as an argument. When the operation either completes or an error
is raised, the callback function is called with
the Error object (if any) passed as the first argument. If no error was
raised, the first argument will be passed as `null`.

```js
const fs = require('fs');

function nodeStyleCallback(err, data) {
function errorFirstCallback(err, data) {
if (err) {
console.error('There was an error', err);
return;
}
console.log(data);
}

fs.readFile('/some/file/that/does-not-exist', nodeStyleCallback);
fs.readFile('/some/file/that/does-exist', nodeStyleCallback);
fs.readFile('/some/file/that/does-not-exist', errorFirstCallback);
fs.readFile('/some/file/that/does-exist', errorFirstCallback);
```

The JavaScript `try / catch` mechanism **cannot** be used to intercept errors
generated by asynchronous APIs. A common mistake for beginners is to try to
use `throw` inside a Node.js style callback:
use `throw` inside an error-first callback:

```js
// THIS WILL NOT WORK:
Expand Down
17 changes: 9 additions & 8 deletions doc/api/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ added: v8.2.0
* Returns: {Function} a callback style function

Takes an `async` function (or a function that returns a Promise) and returns a
function following the Node.js error first callback style. In the callback, the
first argument will be the rejection reason (or `null` if the Promise resolved),
and the second argument will be the resolved value.
function following the error-first callback style, i.e. taking
a `(err, value) => ...` callback as the last argument. In the callback, the
first argument will be the rejection reason (or `null` if the Promise
resolved), and the second argument will be the resolved value.

For example:

Expand Down Expand Up @@ -489,8 +490,8 @@ added: v8.0.0
* `original` {Function}
* Returns: {Function}

Takes a function following the common Node.js callback style, i.e. taking a
`(err, value) => ...` callback as the last argument, and returns a version
Takes a function following the common error-first callback style, i.e. taking
a `(err, value) => ...` callback as the last argument, and returns a version
that returns promises.

For example:
Expand Down Expand Up @@ -526,9 +527,9 @@ will return its value, see [Custom promisified functions][].

`promisify()` assumes that `original` is a function taking a callback as its
final argument in all cases. If `original` is not a function, `promisify()`
will throw an error. If `original` is a function but its last argument is not a
Node.js style callback, it will still be passed a Node.js style callback
as its last argument.
will throw an error. If `original` is a function but its last argument is not
an error-first callback, it will still be passed an error-first
callback as its last argument.

### Custom promisified functions

Expand Down

0 comments on commit e059bc5

Please sign in to comment.