Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setImmediate() ,process.nextTick() understanding #4366

Open
maccrin opened this issue Mar 26, 2024 · 2 comments
Open

setImmediate() ,process.nextTick() understanding #4366

maccrin opened this issue Mar 26, 2024 · 2 comments
Labels

Comments

@maccrin
Copy link

maccrin commented Mar 26, 2024

Enter your suggestions in details:

when I run the below code in ES module

const start = () => {
    console.log('start');
    setImmediate(baz);
    setTimeout(() => console.log('timeout', 0))
    new Promise((resolve, reject) => {
        resolve('bar');
    }).then(resolve => {
        console.log(resolve);
        process.nextTick(zoo);
    });
    process.nextTick(foo);
};

start();

output

start
bar
foo
zoo
baz

As per the documentation
https://github.com/nodejs/nodejs.org/blob/main/pages/en/learn/asynchronous-work/understanding-setimmediate.md
It should be

start
foo
bar
zoo
baz

I got the expected output as per documentation when run under CommonJS module.
so is the output is dependent on ES/CommonJS, it would be helpful if that is mentioned in the documentation.

@thisalihassan
Copy link

@maccrin actually same I am on ubuntu node v20.11.1
if I paste the code in test.js and run
node test.js
my output is similar "bar" comes before foo sometimes. however this is not always the case you can try again it will be different "foo" will come before "bar". so I am not sure what could be the reason here. If someone explains this weird behavior that would be great.

but when I run the code in the node shell it gives the expected out like in the article

I also tested in mac in both ways the expected output (from the article) was similar in both cases

@bmuenzenmeyer bmuenzenmeyer transferred this issue from nodejs/nodejs.org Mar 27, 2024
@Trott
Copy link
Member

Trott commented Apr 5, 2024

@nodejs/timers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants