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

Abort - takes time to reject pending promises #54

Open
sudhakarnraju opened this issue Oct 25, 2018 · 1 comment
Open

Abort - takes time to reject pending promises #54

sudhakarnraju opened this issue Oct 25, 2018 · 1 comment
Labels
needs more info Needs more info from reporter

Comments

@sudhakarnraju
Copy link

sudhakarnraju commented Oct 25, 2018

I am using AbortController to abort my promises. However when aborting, rather than immediate rejection of all pending promises, the library continues to throttle and reject ends up taking more time. E.g if there are 50 promises with throttle of 1 req/sec, you'll end up with 50 seconds for abort to finish.

Below is the code sequence I run. Is there anything I'm missing?

const promiseThrottle = new PromiseThrottle({
  requestsPerSecond: 1,
  promiseImplementation: Promise // the Promise library you are using
});
let controller = new AbortController();
let signal = controller.signal;
let myPromises = [];
myPromises.push[promiseThrottle.add(my_axios_api, { signal })];
myPromises.push[promiseThrottle.add(my_axios_api { signal })];
return Promise.all(myPromises);
// and elsewhere , i loop and poll, if cancel should be aborted
this.cancelPoller = setInterval(() => {
  if (this.shouldCancelUpload()) {
    controller.abort();
  }
}, 1000); //check every 1 second if upload has to be cancelled
@JMPerez
Copy link
Owner

JMPerez commented Nov 3, 2018

At the moment if you pass the signal to a single promise it will cancel that promise. The library doesn't take into account the case in which you are using the same signal for several promises added individually.

I think you can apply the example shown in the README file, where the signal is passed to the whole set of promises queued.

const promiseThrottle = new PromiseThrottle({
  requestsPerSecond: 1,
  promiseImplementation: Promise
});
let controller = new AbortController();
let signal = controller.signal;
return promiseThrottle.addAll([my_axios_api, my_axios_api], {signal});

Let me know if that works!

@JMPerez JMPerez added the needs more info Needs more info from reporter label Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs more info Needs more info from reporter
Projects
None yet
Development

No branches or pull requests

2 participants