Demonstrate doing asynchronous work during a forced synchronous event.
START AN EMPTY server.js FILE.
Add an "exit" handler to the node process that tries to set a timeout to log a
message.
process.on('exit', () => {
console.log('Got exit.');
setTimeout(() => {
console.log('Exit done.');
}, 3_000);
process.exit();
});node server.jsNotice that we only get 1 of the 2 messages in the code and it doesn't wait 3 seconds.
Create a Int32Array using a SharedArrayBuffer and force the process to wait
using that.
process.on('exit', () => {
console.log('Got exit.');
const lock = new Int32Array(new SharedArrayBuffer(4));
Atomics.wait(lock, 0, 0, 3_000);
console.log('Exit done.');
process.exit();
});Create a file server_worker.js that can notify workerData.lock after a
setTimeout.
// server_worker.js
import { workerData } from 'worker_threads';
setTimeout(() => {
workerData.lock = 1;
Atomics.notify(workerData.lock, 0, 1);
}, 3_000);Invoke server_worker.js during the "exit" event to perform async work.
import { Worker } from 'worker_threads';
process.on('exit', () => {
console.log('Got exit.');
const lock = new Int32Array(new SharedArrayBuffer(4));
new Worker(
new URL('./server_worker.js', import.meta.url), {
workerData: { lock }
});
Atomics.wait(lock, 0, 0, Infinity);
console.log('Exit done.');
process.exit();
});