Skip to content

doc: process.nextTick() example question #12280

Description

@vsemozhetbyt

This section says:

// WARNING!  DO NOT USE!  BAD UNSAFE HAZARD!
function maybeSync(arg, cb) {
  if (arg) {
    cb();
    return;
  }

  fs.stat('file', cb);
}

This API is hazardous because in the following case:

maybeSync(true, () => {
  foo();
});
bar();

It is not clear whether foo() or bar() will be called first.

For me, if arg is true, the order is unambiguously maybeSync() -> cb() -> foo() -> bar(), i.e. no async at all. What do I miss?

Activity

  1. added
    processIssues and PRs related to the process subsystem.
    questionIssues asking questions about Node.js.
    on Apr 8, 2017
  2. bnoordhuis commented on Apr 8, 2017

    @bnoordhuis
    Member

    Replace true with Math.random() > 0.5. The order of events is now indeterminate.

  3. vsemozhetbyt commented on Apr 8, 2017

    @vsemozhetbyt
    ContributorAuthor

    So is this a confusing wording in the doc ('in the following case')? Should the true in the example be replaced by this Math.random() > 0.5 or something like maybeTrue?

  4. bnoordhuis commented on Apr 8, 2017

    @bnoordhuis
    Member

    So is this a confusing wording in the doc ('in the following case')?

    I'd say that's axiomatically true since it clearly confused you. :-)

    Should the true in the example be replaced by this Math.random() > 0.5

    I agree the current example is not great so yes, I'd update it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    processIssues and PRs related to the process subsystem.questionIssues asking questions about Node.js.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions