1const app = require('fastify')();
2
3app.get('/api/user/:id', async (req, reply) => {
4 console.log('Handler called');
5
6 // Schedule microtasks
7 process.nextTick(() => {
8 console.log('nextTick: validate session');
9 });
10
11 const userPromise = db.query(
12 'SELECT * FROM users WHERE id = $1',
13 [req.params.id]
14 );
15
16 // Schedule timer for timeout
17 const timeout = setTimeout(() => {
18 console.log('Timeout: request too slow');
19 }, 5000);
20
21 // Await DB result (I/O)
22 const user = await userPromise;
23
24 // After I/O: schedule cleanup
25 setImmediate(() => {
26 console.log('setImmediate: cache update');
27 });
28
29 clearTimeout(timeout);
30 reply.send({ user });
31});
32
33app.listen({ port: 3000 });