为什么绑定(bind)函数对于以下代码停止工作?
function exitHandler(options, err) {
console.log('exit');
if (options.cleanup) console.log('clean');
if (err) console.log(err.stack);
if (options.exit) process.exit();
}
//do something when app is closing
//process.on('exit', exitHandler.bind(null,{cleanup:true})); process.exit()
// or
process.on('exit', function() {
exitHandler.bind(null,{cleanup:true})
});
如果我取消注释 process.on('exit', exitHandler.bind...
行,它会正常工作。
最佳答案
我认为这是因为绑定(bind)创建了一个新函数,所以在第二个示例中它实际上并没有触发该函数。在第一种情况下,它确实会被解雇。
关于javascript - 为什么 NodeJS 的绑定(bind)函数在这种情况下停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48968240/