Skip to content

process, doc: discourage use of process.exit() for control flow in docs #7333

@eljefedelrodeodeljefe

Description

@eljefedelrodeodeljefe

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.

Ref: #6980, #6456

cc @Fishrock123

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to the documentations.processIssues and PRs related to the process subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions