-
-
Notifications
You must be signed in to change notification settings - Fork 33.3k
Description
Userland library authors have a pattern for CLIs that has lead to various issues during the introduction of v6. We had a heated discussion that I would summarize as: the below pattern should have been used in like this, since it actually never ensured to deliver what it promised; but core has a historic responsibility of not breaking such widely adopted patterns.
process.on('exit', () => {
// do some post action here later
})
function doSomething() {
for (var i = 0; i < 1000; i++) {
process.stdout.write('some result' + i + '\n')
}
// decide that the execution of the CLI should end here
process.exit()
}
The problem with this that stdout doesn't get flushed on process.exit()
, resulting in not all all 1000 calls being printed. This became apparent in v6. Eventually this calls exit(3). In any good c++ practice exit(3) is discouraged, since functions scopes are not guaranteed to unwind properly.
Imo, authors should at be just return from functions, from top scope, or use proper event emitters.
Someone could open a doc PR.
cc @Fishrock123