@@ -66,7 +66,7 @@ $RefParser.parse = function (path, schema, options, callback) {
6666 * @param {function } [callback] - An error-first callback. The second parameter is the parsed JSON schema object.
6767 * @returns {Promise } - The returned promise resolves with the parsed JSON schema object.
6868 */
69- $RefParser . prototype . parse = function ( path , schema , options , callback ) {
69+ $RefParser . prototype . parse = async function ( path , schema , options , callback ) {
7070 let args = normalizeArgs ( arguments ) ;
7171 let promise ;
7272
@@ -108,19 +108,20 @@ $RefParser.prototype.parse = function (path, schema, options, callback) {
108108 }
109109
110110 let me = this ;
111- return promise
112- . then ( ( result ) => {
113- if ( ! result || typeof result !== "object" || Buffer . isBuffer ( result ) ) {
114- throw ono . syntax ( `"${ me . $refs . _root$Ref . path || result } " is not a valid JSON Schema` ) ;
115- }
116- else {
117- me . schema = result ;
118- return maybe ( args . callback , Promise . resolve ( me . schema ) ) ;
119- }
120- } )
121- . catch ( ( e ) => {
122- return maybe ( args . callback , Promise . reject ( e ) ) ;
123- } ) ;
111+ try {
112+ let result = await promise ;
113+
114+ if ( ! result || typeof result !== "object" || Buffer . isBuffer ( result ) ) {
115+ throw ono . syntax ( `"${ me . $refs . _root$Ref . path || result } " is not a valid JSON Schema` ) ;
116+ }
117+ else {
118+ me . schema = result ;
119+ return maybe ( args . callback , Promise . resolve ( me . schema ) ) ;
120+ }
121+ }
122+ catch ( e ) {
123+ return maybe ( args . callback , Promise . reject ( e ) ) ;
124+ }
124125} ;
125126
126127/**
@@ -155,20 +156,18 @@ $RefParser.resolve = function (path, schema, options, callback) {
155156 * @returns {Promise }
156157 * The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
157158 */
158- $RefParser . prototype . resolve = function ( path , schema , options , callback ) {
159+ $RefParser . prototype . resolve = async function ( path , schema , options , callback ) {
159160 let me = this ;
160161 let args = normalizeArgs ( arguments ) ;
161162
162- return this . parse ( args . path , args . schema , args . options )
163- . then ( ( ) => {
164- return resolveExternal ( me , args . options ) ;
165- } )
166- . then ( ( ) => {
167- return maybe ( args . callback , Promise . resolve ( me . $refs ) ) ;
168- } )
169- . catch ( ( err ) => {
170- return maybe ( args . callback , Promise . reject ( err ) ) ;
171- } ) ;
163+ try {
164+ await this . parse ( args . path , args . schema , args . options ) ;
165+ await resolveExternal ( me , args . options ) ;
166+ return maybe ( args . callback , Promise . resolve ( me . $refs ) ) ;
167+ }
168+ catch ( err ) {
169+ return maybe ( args . callback , Promise . reject ( err ) ) ;
170+ }
172171} ;
173172
174173/**
@@ -199,18 +198,18 @@ $RefParser.bundle = function (path, schema, options, callback) {
199198 * @param {function } [callback] - An error-first callback. The second parameter is the bundled JSON schema object
200199 * @returns {Promise } - The returned promise resolves with the bundled JSON schema object.
201200 */
202- $RefParser . prototype . bundle = function ( path , schema , options , callback ) {
201+ $RefParser . prototype . bundle = async function ( path , schema , options , callback ) {
203202 let me = this ;
204203 let args = normalizeArgs ( arguments ) ;
205204
206- return this . resolve ( args . path , args . schema , args . options )
207- . then ( ( ) => {
208- bundle ( me , args . options ) ;
209- return maybe ( args . callback , Promise . resolve ( me . schema ) ) ;
210- } )
211- . catch ( ( err ) => {
212- return maybe ( args . callback , Promise . reject ( err ) ) ;
213- } ) ;
205+ try {
206+ await this . resolve ( args . path , args . schema , args . options ) ;
207+ bundle ( me , args . options ) ;
208+ return maybe ( args . callback , Promise . resolve ( me . schema ) ) ;
209+ }
210+ catch ( err ) {
211+ return maybe ( args . callback , Promise . reject ( err ) ) ;
212+ }
214213} ;
215214
216215/**
@@ -239,16 +238,16 @@ $RefParser.dereference = function (path, schema, options, callback) {
239238 * @param {function } [callback] - An error-first callback. The second parameter is the dereferenced JSON schema object
240239 * @returns {Promise } - The returned promise resolves with the dereferenced JSON schema object.
241240 */
242- $RefParser . prototype . dereference = function ( path , schema , options , callback ) {
241+ $RefParser . prototype . dereference = async function ( path , schema , options , callback ) {
243242 let me = this ;
244243 let args = normalizeArgs ( arguments ) ;
245244
246- return this . resolve ( args . path , args . schema , args . options )
247- . then ( ( ) => {
248- dereference ( me , args . options ) ;
249- return maybe ( args . callback , Promise . resolve ( me . schema ) ) ;
250- } )
251- . catch ( ( err ) => {
252- return maybe ( args . callback , Promise . reject ( err ) ) ;
253- } ) ;
245+ try {
246+ await this . resolve ( args . path , args . schema , args . options ) ;
247+ dereference ( me , args . options ) ;
248+ return maybe ( args . callback , Promise . resolve ( me . schema ) ) ;
249+ }
250+ catch ( err ) {
251+ return maybe ( args . callback , Promise . reject ( err ) ) ;
252+ }
254253} ;
0 commit comments