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

event loop tick id #1740

Closed
ORESoftware opened this issue Jan 29, 2019 · 6 comments
Closed

event loop tick id #1740

ORESoftware opened this issue Jan 29, 2019 · 6 comments

Comments

@ORESoftware
Copy link

ORESoftware commented Jan 29, 2019

On Node.js version 10

Is there a way to access the event loop tick id? The use case is simple: When writing a library you can easily detect whether the user fired a callback synchronously or asynchronously - ultimately the purpose of detecting this is to prevent Zalgo and keep APIs/libraries consistent and performant.

For example, with the async libary, a user can do this:

async.parallel([
   cb => cb(),
   cb => process.nextTick(cb)
]);

to my knowledge, the async lib doesn't call process.nextTick on behalf of the user when the user makes this mistake (but the async lib could do that if it wanted to and there was an easily and performant way to check if the user fired the callback in the same tick).

So I am thinking of Node.js API that can allow for detecting the current event loop tick, something like this:

const id = process.currentTickId();
 /// ...do some stuff then...
if(id !== process.currentTickId()){
  throw new Error('You cannot call this code asynchronously!');
}

the idea is to prevent someone from calling certain code asynchronously. the only way I know how to do this now is with this hack:

let before = false;
process.nextTick(() => {
   before = true;
});
process.nextTick(() => {
 if(before === true){
   throw new Error('You cannot call this code asynchronously!');
 }
}

but that's not very performant or convenient. the hack simply relies on the fact that the first process.nextTick call will happen before any other async callbacks get fired.

@gireeshpunathil
Copy link
Member

there is a time field in the event loop that gets updated everytime it enters the polling loop. This can be meaningfully interpreted as an id. Though I am not sure whether this is exposed through any JS API.

nodejs/node#25378 seems to be a good candidate for exposing it, if at all. @jasnell

@ChALkeR
Copy link
Member

ChALkeR commented Jan 29, 2019

nodejs/node#22584 could use similar functionality too.

@ORESoftware
Copy link
Author

@ChALkeR can you elaborate on that here?

@Hakerh400
Copy link

Try this:

process.currentTickId = (a =>
  (b => () => a = a || [b(() => {})])
  (process.nextTick = ((b, c) => (...d) => (
    c || b(() => a = c = 0, c = 1),
    b.apply(process, d)
  ))(process.nextTick))
)();

@PoojaDurgad
Copy link

@ORESoftware , is this issue resolved? any updates?

@gireeshpunathil
Copy link
Member

inactive, and / or resolved, closing. pls feel free to reopen if required

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

No branches or pull requests

5 participants