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

Passing context info in onTick function #47

Closed
ibansal opened this issue Sep 11, 2012 · 14 comments
Closed

Passing context info in onTick function #47

ibansal opened this issue Sep 11, 2012 · 14 comments

Comments

@ibansal
Copy link

ibansal commented Sep 11, 2012

We need to pass context info in onTick function the way this is implemented in setInterval, any support for this.

@ncb000gt
Copy link
Member

Hmm. I can see this being useful. The tradeoff is that you do not get to call this.stop() from within the callback. I think this is probably fine.

It will default to calling the onComplete function within the scope of the job.

Thoughts?

ncb000gt added a commit that referenced this issue Sep 11, 2012
It seems to be a nice feature that the oncomplete method be allowed to
have a specific context within which to execute (GH-47).

Signed-off-by: Nick Campbell <[email protected]>
@ncb000gt
Copy link
Member

I've got one other feature I want to work out before pushing a new version. Once that is done I'll publish. For now you can use the code from master to accomplish what you want.

Also, I just noticed that I called it onComplete in the in the commit and comment...that is just what it is called internally in the code. The tests are for the onTick function.

@ncb000gt
Copy link
Member

This has been published to npm in v1.0.1.

@ibansal
Copy link
Author

ibansal commented Sep 14, 2012

Great ... thanks...

@Danielv3
Copy link

Hello. My code is using 1.3.0 version and I'm not able to send a custom context to the onTick function. If I don't send any context, the this.stop() method cannot be called neither because stop is not a function... Any ideas? thnks.

@ncb000gt
Copy link
Member

@Danielv3 do you have a code sample you're using?

@Danielv3
Copy link

I solved the issue. You need to use "function() {}" instead of the newly arrow functions "() => {}" in order to be able to use "this" as context. In the newer arrow format you don't receive the context in this.

@charliecode
Copy link

I also had a similar issue as @Danielv3 while using a fat arrow function within a class on the onTick method. Because fat arrow functions get their "this" value from their enclosing lexical context and not the actual context they are within you don't actually get the cronJob attached to the "this" keyword inside the onTick function. Only a suggestion, but with more and more people using arrow functions by default it might be a good idea to add something to the docs about this. Arrow functions should not be used for the onTick method, only regular javascript functions.

@ncb000gt
Copy link
Member

ncb000gt commented Feb 9, 2019

@charliecode I added a note, thanks for the suggestion.

@NonPolynomial
Copy link

Is there any plans to support arrow functions or are there any particular reasons against them?
It should be easy to pass the this context as a parameter to arrow functions.
E.g.

onTick(this)

@ncb000gt
Copy link
Member

ncb000gt commented Apr 23, 2019 via email

@NonPolynomial
Copy link

I'll have a look into it.

@NonPolynomial
Copy link

NonPolynomial commented Apr 23, 2019

@ncb000gt currently there is the onComplete function that is passed to the onTick function.
Should that stay unchanged?
Users could instead call either this.onComplete() or - after my change - job.onComplete() to execute that function.

Context

@Kapsonfire-DE
Copy link

its more easy to use .apply or .call and pass the correct this param

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

6 participants