Skip to content

Instantly share code, notes, and snippets.

@ponkys
Last active November 10, 2021 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponkys/89668056860e50aa2a1b903b8caa4cba to your computer and use it in GitHub Desktop.
Save ponkys/89668056860e50aa2a1b903b8caa4cba to your computer and use it in GitHub Desktop.
Emulate a timeout
const fastify = require('fastify')({ logger: true })
var timer = {};
function delay(callback, length){
if(!timer.startTime){
timer.startTime = new Date().getTime();
timer.callback = callback;
timer.length = length;
}
if(new Date().getTime() < timer.startTime + timer.length){
process.nextTick(delay);
} else {
timer.callback();
timer = {};
}
}
// Declare a route
fastify.post('/', async (request, reply) => {
delay(function(){
console.log("callback");
reply.send({stdout: "hello"})
}, 10000);
})
// Declare a route
fastify.get('/en/home', async (request, reply) => {
delay(function(){
console.log("callback");
reply.send({stdout: "hello"})
}, 10000);
})
// Run the server!
const start = async () => {
try {
await fastify.listen(3001)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment