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

how should i use clearqueue? #49

Closed
WystanW opened this issue Jan 25, 2021 · 5 comments
Closed

how should i use clearqueue? #49

WystanW opened this issue Jan 25, 2021 · 5 comments

Comments

@WystanW
Copy link

WystanW commented Jan 25, 2021

I want to stop the pending tasks

@sindresorhus
Copy link
Owner

I don't understand the question. I think the docs are pretty clear about it.

@WystanW
Copy link
Author

WystanW commented Jan 25, 2021

I don't understand the question. I think the docs are pretty clear about it.

I have an variable named 'switch' and a vue watcher to watch 'switch' ; if 'switch' is false,then I want to stop all the tasks pending in the limit queue.

watch: { switch: { handler(next, prev) { if (this.switch) { this.limit.clearQueue() } } }

but the tasks is still running to the end. The code cannot stop the tasks in the queue

@sindresorhus
Copy link
Owner

but the tasks is still running to the end.

You cannot stop the "active" tasks. .clearQueue() only clears the pending (yet to run) tasks. This is a limitation of promises. Once a promise starts, it cannot be stopped/canceled from the outside.

@WystanW
Copy link
Author

WystanW commented Jan 25, 2021

but the tasks is still running to the end.

You cannot stop the "active" tasks. .clearQueue() only clears the pending (yet to run) tasks. This is a limitation of promises. Once a promise starts, it cannot be stopped/canceled from the outside.

Thanks for reply!!!

Yes! I just want to stop the pending tasks . As your example

const pLimit = require('p-limit');

const limit = pLimit(1);

const input = [
	limit(() => fetchSomething('foo')),
	limit(() => fetchSomething('bar')),
	limit(() => doSomething())
        // ......if there are 1000 functions
];

(async () => {
	// Only one promise is run at once
	const result = await Promise.all(input);
	console.log(result);
})();

if I have 1000 limit(() => myfunction())
then the code const result = await Promise.all(input); need time to finish.

In this period, may be there are 500 pending rasks, 1 running tasks, and 499 finished tasks. I just want to stop these 500 pending tasks when a variable called "switch" is true. But i don't know how to use limit.clearQueue()

if (this.switch) {
   this.limit.clearQueue() 
} 

Could U give me an example?

@sindresorhus
Copy link
Owner

You could do something like this:

const pLimit = require('p-limit');

const limit = pLimit(1);

const check = fn => {
	if (switch) {
	   limit.clearQueue();
	}

	fn();
};

const input = [
	limit(check(() => fetchSomething('foo'))),
	limit(check(() => fetchSomething('bar'))),
	limit(check(() => doSomething()))
        // ......if there are 1000 functions
];

(async () => {
	// Only one promise is run at once
	const result = await Promise.all(input);
	console.log(result);
})();

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