Last active
November 10, 2021 21:49
-
-
Save ponkys/89668056860e50aa2a1b903b8caa4cba to your computer and use it in GitHub Desktop.
Emulate a timeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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