MCPcopy
hub / github.com/fastify/fastify / ready

Function ready

fastify.js:507–562  ·  view source on GitHub ↗
(cb)

Source from the content-addressed store, hash-verified

505 }
506
507 function ready (cb) {
508 if (this[kState].readyResolver !== null) {
509 if (cb != null) {
510 this[kState].readyResolver.promise.then(() => cb(null, fastify), cb)
511 return
512 }
513
514 return this[kState].readyResolver.promise
515 }
516
517 // run the hooks after returning the promise
518 process.nextTick(runHooks)
519
520 // Create a promise no matter what
521 // It will work as a barrier for all the .ready() calls (ensuring single hook execution)
522 // as well as a flow control mechanism to chain cbs and further
523 // promises
524 this[kState].readyResolver = PonyPromise.withResolvers()
525
526 if (!cb) {
527 return this[kState].readyResolver.promise
528 } else {
529 this[kState].readyResolver.promise.then(() => cb(null, fastify), cb)
530 }
531
532 function runHooks () {
533 // start loading
534 fastify[kAvvioBoot]((err, done) => {
535 if (err || fastify[kState].started || fastify[kState].ready || fastify[kState].booting) {
536 manageErr(err)
537 } else {
538 fastify[kState].booting = true
539 hookRunnerApplication('onReady', fastify[kAvvioBoot], fastify, manageErr)
540 }
541 done()
542 })
543 }
544
545 function manageErr (err) {
546 // If the error comes out of Avvio's Error codes
547 // We create a make and preserve the previous error
548 // as cause
549 err = err != null && AVVIO_ERRORS_MAP[err.code] != null
550 ? appendStackTrace(err, new AVVIO_ERRORS_MAP[err.code](err.message))
551 : err
552
553 if (err) {
554 return fastify[kState].readyResolver.reject(err)
555 }
556
557 fastify[kState].readyResolver.resolve(fastify)
558 fastify[kState].booting = false
559 fastify[kState].ready = true
560 fastify[kState].readyResolver = null
561 }
562 }
563
564 // Used exclusively in TypeScript contexts to enable auto type inference from JSON schema.

Callers

nothing calls this directly

Calls 2

cbFunction · 0.85
thenMethod · 0.80

Tested by

no test coverage detected