an async queue for fastify
``sh`
npm i @autotelic/fastify-queue
#### Example
`js
const fastifyQueue = require('@autotelic/fastify-queue')
function myRoute (fastify, opts) {
fastify.register(fastifyQueue)
fastify.post('/example', async (request, reply) => {
reply.queue.add('async-action-one', reply.asyncAction())
reply.queue.add('async-action-two', reply.asyncActionTwo())
reply.queue.add('async-action-three', reply.asyncActionThree())
// Manual resolution optional - queue will otherwise resolve in onResponse hook.
const results = await reply.queue.resolve()
reply.status(200)
})
}
`
fastify-queue accepts the following optional configuration:
- #### initializer: (key: string, value: any, reply: FastifyReply) => any | Promise
- Called each time an item is added to the queue. The value returned will be the value added to the queue.
- #### resolver: (key: string, value: any, reply: FastifyReply) => anyvalue
- Called each time an item in the queue is resolved. is the resolved value of an item in the queue.
- #### onError: (error: Error, reply: FastifyReply) => anyfastify.log.error(error)
- Called if an error occurs while resolving the queue. Defaults to .
- #### onResolved: (result: any, reply: FastifyReply) => voidresult
- Called after the queue has successfully resolved. is the resolved queue.
- #### queueName: string'queue'
- Determines the name of the reply decorator added by the plugin. Defaults to .
- #### concurrency: numberInfinity
- Max number of concurrently pending promises. Defaults to .options
- This value is passed directly to p-props param.
- #### stopOnError: booleanfalse
- When set to , errors thrown while resolving items in the queue will be accumulated. Once all items in the queue have settled, a single aggregated error will be thrown.options
- This value is passed directly to p-props param.
fastify-queue adds a queue (or the value of opts.queueName) reply decorator containing the following properties:
- #### add: (key: string, value: any) => void
- Used to add items to the queue.
- #### resolve: (cb?: (result: any, reply: FastifyReply) => void) => voidcb
- Used to manually resolve the queue. defaults to opts.onResolved.onResponse
- fastify-queue will automatically resolve the queue in an hook, if this has not already been called.
- #### contents: MapMap` instance.
- The queue