You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 12, 2026. It is now read-only.
All of JSON Schema $Ref Parser's methods are available as static (class) methods, and as instance methods. The static methods simply create a new [`$RefParser`](ref-parser.md) instance and then call the corresponding instance method. Thus, the following line...
32
+
33
+
All of JSON Schema $Ref Parser's methods are available as static (class) methods, and as instance methods. The static methods simply create a new [`$RefParser`](ref-parser.md) instance and then call the corresponding instance method. Thus, the following line...
The difference is that in the second example you now have a reference to `parser`, which means you can access the results ([`parser.schema`](ref-parser.md#schema) and [`parser.$refs`](ref-parser.md#refs)) anytime you want, rather than just in the callback function.
48
47
49
-
50
48
### Callbacks vs. Promises
51
-
Many people prefer [Promise syntax](http://javascriptplayground.com/blog/2015/02/promises/) or `async`/`await` instead of callbacks. JSON Schema $Ref Parser allows you to use whichever one you prefer.
52
49
53
-
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 a Promise.
50
+
Many people prefer [Promise syntax](http://javascriptplayground.com/blog/2015/02/promises/) or `async`/`await` instead of callbacks. JSON Schema $Ref Parser allows you to use whichever one you prefer.
51
+
52
+
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 a Promise.
54
53
55
54
The following two examples are equivalent:
56
55
57
56
```javascript
58
57
// Callback syntax
59
58
$RefParser.dereference(mySchema, (err, api) => {
60
-
if (err) {
61
-
// Error
62
-
}
63
-
else {
64
-
// Success
65
-
}
59
+
if (err) {
60
+
// Error
61
+
} else {
62
+
// Success
63
+
}
66
64
});
67
65
```
68
66
69
67
```javascript
70
68
try {
71
-
// async/await syntax
72
-
let api =await$RefParser.dereference(mySchema);
69
+
// async/await syntax
70
+
let api =await$RefParser.dereference(mySchema);
73
71
74
-
// Success
75
-
}
76
-
catch (err) {
77
-
// Error
72
+
// Success
73
+
} catch (err) {
74
+
// Error
78
75
}
79
76
```
80
77
81
-
82
78
### Circular $Refs
83
-
JSON Schema files can contain [circular $ref pointers](https://gist.github.com/JamesMessinger/d18278935fc73e3a0ee1), and JSON Schema $Ref Parser fully supports them. Circular references can be resolved and dereferenced just like any other reference. However, if you intend to serialize the dereferenced schema as JSON, then you should be aware that [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) does not support circular references by default, so you will need to [use a custom replacer function](https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json).
79
+
80
+
JSON Schema files can contain [circular $ref pointers](https://gist.github.com/JamesMessinger/d18278935fc73e3a0ee1), and JSON Schema $Ref Parser fully supports them. Circular references can be resolved and dereferenced just like any other reference. However, if you intend to serialize the dereferenced schema as JSON, then you should be aware that [`JSON.stringify`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) does not support circular references by default, so you will need to [use a custom replacer function](https://stackoverflow.com/questions/11616630/json-stringify-avoid-typeerror-converting-circular-structure-to-json).
84
81
85
82
You can disable circular references by setting the [`dereference.circular`](options.md) option to `false`. Then, if a circular reference is found, a `ReferenceError` will be thrown.
86
83
87
-
Or you can choose to just ignore circular references altogether by setting the [`dereference.circular`](options.md) option to `"ignore"`. In this case, all non-circular references will still be dereferenced as normal, but any circular references will remain in the schema.
84
+
Or you can choose to just ignore circular references altogether by setting the [`dereference.circular`](options.md) option to `"ignore"`. In this case, all non-circular references will still be dereferenced as normal, but any circular references will remain in the schema.
88
85
89
-
Another option is to use the [`bundle`](ref-parser.md#bundleschema-options-callback) method rather than the [`dereference`](ref-parser.md#dereferenceschema-options-callback) method. Bundling does _not_ result in circular references, because it simply converts _external_`$ref` pointers to _internal_ ones.
86
+
Another option is to use the [`bundle`](ref-parser.md#bundleschema-options-callback) method rather than the [`dereference`](ref-parser.md#dereferenceschema-options-callback) method. Bundling does _not_ result in circular references, because it simply converts _external_`$ref` pointers to _internal_ ones.
0 commit comments