MCPcopy Create free account
hub / github.com/fastify/fastify / ready

Function ready

fastify.js:509–564  ·  view source on GitHub ↗
(cb)

Source from the content-addressed store, hash-verified

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