在 Node 应用程序中,所需的脚本是否有可能停止其父脚本的进一步执行?
假设a.js
需要b.js
。 b.js
是否可以停止 a.js
的处理?
a.js
require('b.js')
// some other code that I might not want to execute
b.js
if (/* we do want `a.js` to continue */) {
return
} else {
// somehow stop `a.js` from being further processed
}
理想情况下,该解决方案不会涉及生成子进程:)对此的任何见解将不胜感激!谢谢!
最佳答案
导出一个抛出异常的函数:
module.exports = function() {
// In case you want to end the parents execution:
throw new Error("I just want to be mean");
};
然后将其用作:
require("b.js")();
console.log("this will never happen");
关于javascript - 所需的脚本可以停止其父脚本的执行吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53796145/