eventloop_unblocking_event_loop.js

PHOTO EMBED

Thu Jan 28 2021 07:13:04 GMT+0000 (Coordinated Universal Time)

Saved by @noobj

fastify.get('/unblock-event-loop', async (_request, reply) => {
  const hash = crypto.createHash('sha256');
  const numberOfHasUpdates = 10e6;

  const breathSpace = async delayInS =>
    new Promise(res => setTimeout(() => res(), delayInS * 1000));

  for (let iter = 0; iter < numberOfHasUpdates; iter++) {
    await hash.update(getRandomString());
    await breathSpace(0);
  }
  reply.send({ data: 'Finished long process' });
});
content_copyCOPY