Skip to content

Commit 2bfdea0

Browse files
Documented changes to the promise-vs-callback functionality
1 parent ac37aa5 commit 2bfdea0

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

docs/README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,32 @@ The difference is that in the second example you now have a reference to `parser
5454

5555

5656
### Callbacks vs. Promises
57-
Many people prefer [ES6 Promise syntax](http://javascriptplayground.com/blog/2015/02/promises/) instead of callbacks. JSON Schema $Ref Parser allows you to use whichever one you prefer. Every method accepts an optional callback _and_ returns a Promise. So pick your poison.
57+
Many people prefer [ES6 Promise syntax](http://javascriptplayground.com/blog/2015/02/promises/) instead of callbacks. JSON Schema $Ref Parser allows you to use whichever one you prefer.
58+
59+
If you pass a callback function to any method, then the method will call the callback using the Node.js error-first convention. If you do _not_ pass a callback function, then the method will return an ES6 Promise.
60+
61+
The following two examples are equivalent:
5862

5963
```javascript
64+
// Callback syntax
6065
$RefParser.dereference(mySchema, function(err, api) {
61-
// Callback (success or error)
62-
})
66+
if (err) {
67+
// Error
68+
}
69+
else {
70+
// Success
71+
}
72+
});
73+
```
74+
75+
```javascript
76+
// ES6 Promise syntax
77+
$RefParser.dereference(mySchema)
6378
.then(function(api) {
64-
// Promise (success)
79+
// Success
6580
})
6681
.catch(function(err) {
67-
// Promise (error)
82+
// Error
6883
});
6984
```
7085

0 commit comments

Comments
 (0)