Is there any way for node.js to terminate the function by external force?

lei-sharp-sharp-sharp problem description

the termination here does not refer to the termination when the condition is reached, but by external force.
I use koa as the web service and puppeteer as the crawler to read the list of the database in a loop to query the data of a website. When the list query is completed, it will stop running.
is there a way to stop the crawler function midway and ensure that koa runs properly without killing the entire node process?

Apr.08,2022

you can try child_process (child process) to see if it meets the requirements.
spawns a child thread in the main thread and directly kills the child thread when needed.
below is the test code. A child thread is derived from the main thread and killed after 3 seconds. The child thread has a timer that prints a test
main thread code per second:

const cp = require('child_process');
const grep = cp.fork(`${__dirname}/test.js`);


grep.on('close', (code, signal) => {
  console.log(` ${signal} `);
});

//  SIGHUP 
setTimeout(() => {
  grep.kill('SIGKILL');
}, 3000)

Child thread code:

let timer = setInterval(() => {
  console.log('test');
}, 1000);

the output is
test
test
the child process receives the signal SIGKILL and terminates


seems to have no way, but the solution is also simple. Add a global judgment variable directly, read the value of this variable in the loop, and jump out of the loop to meet your requirements when the variable is set as the termination parameter.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e5cd9b-45700.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e5cd9b-45700.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?