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

No way to stop a running task inside the callback #6

Closed
sayantanhore opened this issue Feb 18, 2016 · 6 comments
Closed

No way to stop a running task inside the callback #6

sayantanhore opened this issue Feb 18, 2016 · 6 comments

Comments

@sayantanhore
Copy link

Hi,

I am using periodic task in my project. I came across a situation where I needed to stop the task from within the callback. As the API suggests to instantiate a task as below,

var task = new PeriodicTask(delay, callback, [context], [args])

I cannot pass the task task instance into the callback to issue task.stop() inside it.

I suggest a workaround here.

var obj = {};
var task = new PeriodicTask(delay, callback, obj, args);
obj.stop = task.stop.bind(task);

If there is any better approach I would appreciate if you inform me. Otherwise would you like to provide a workaround in the API?

Thanks.
Sayantan ...

@cGuille
Copy link
Owner

cGuille commented Feb 18, 2016

Hello,

If you declare your task variable before defining your task's function, then you can use it inside:

var PeriodicTask = require('periodic-task');

var i = 0;
var task = new PeriodicTask(1000, function () {
    console.log(i++);
    if (i > 3) {
        task.stop();
    }
});
task.run();

Thank you for your feedback!

By the way, as stated in the README, have you considered using async-polling instead of this project?
The above sample could be written like this:

var AsyncPolling = require('async-polling');

var i = 0;
AsyncPolling(function (end) {
    console.log(i++);
    if (i > 3) {
        this.stop();
    }
    end();
}, 1000).run();

@sayantanhore
Copy link
Author

Hi,

You are right about the first solution, but my situation is a bit more complicated. I have the callbacks declared before and task is created and run in another function below. Therefore the task is a local variable to that function declared below so somehow I need to pass a this reference to the callback to fire this.stop().

No I have not tried async-polling but will try and if it works (I am sure it will) will replace PeriodicTask with it.

Thanks anyway.

Sayantan ...

@cGuille
Copy link
Owner

cGuille commented Feb 19, 2016

I am going to erase the README of this project, advising to go for async-polling instead. I think it is more relevant to use an asynchrone compliant library in JavaScript, so I prefer to improve async-polling and remove periodic-task.

@sayantanhore
Copy link
Author

Ok, would you consider to mention it as deprecated in the NPM repo? I mean here.

@cGuille
Copy link
Owner

cGuille commented Feb 19, 2016

That's a good idea. I'll do this. Thank you for your advice!

@sayantanhore
Copy link
Author

You are welcome.

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

2 participants