fastify-request-queue
v0.1.7
Published
Sequential execution of methods in Fastify
Downloads
18
Readme
fastify-request-queue
A plugin for Fastify that ensures methods are executed sequentially, preventing race conditions or overlapping executions in your routes. This is particularly useful for scenarios where order of operations matters, such as processing transactions.
Installation
pnpm add fastify-request-queue
# or
yarn add fastify-request-queue
# or
npm install fastify-request-queueUsage
In this example, the /process endpoint will execute requests sequentially, ensuring that each request completes before the next one begins.
CommonJS Example
const fastify = require("fastify")();
const fastifyRequestQueue = require("fastify-request-queue");
fastify.register(fastifyRequestQueue, app => {
app.post("/process", async (request, reply) => {
const { id } = request.body;
// Simulate some async work
await new Promise(resolve => setTimeout(resolve, 1000));
reply.send({ message: `Processed item ${id}` });
});
});
fastify
.listen({ port: 3000, host: "0.0.0.0" })
.then(at => console.log("Server started at", at));TypeScript Example
import Fastify from 'fastify';
import fastifyRequestQueue from 'fastify-request-queue';
const app = Fastify();
app.register(fastifyRequestQueue, (app) => {
app.post<{ Body: { id: number } }>('/process', async (request, reply) => {
const { id } = request.body;
// Simulate some async work
await new Promise((resolve) => setTimeout(resolve, 1000));
return { message: `Processed item ${id}` };
});
});
app
.listen({ port: 3000, host: '0.0.0.0' })
.then(at => console.log("Server started at", at));License
Licensed under MIT.
