Search code examples
node.jsfunctiondelayabort

Is there a way of stopping a function after a delay? Nodejs


Is it possible to abort a function after a delay? Something like:

function f() {
    //Do some process here
}

after(10000).stop(f)

Any idea about how to do that? Thank you.


Solution

  • Try cluster module like this

    const cluster = require('cluster');
    
    if (cluster.isMaster) {
        const fn = cluster.fork();
        setTimeout(_=>fn.process.kill(), 300);
    } else {
        for(;;) {
           process.stdout.write('#');
        }
    }