Git Product home page Git Product logo

Comments (4)

markrian avatar markrian commented on May 18, 2024

To clarify why I find this behaviour surprising, imagine that I'd want to put do some clean up in await/awaitAll that is only worth running when all async tasks are truly finished.

from d3-queue.

mbostock avatar mbostock commented on May 18, 2024

This is the expected behavior.

You’ve defined a queue with a concurrency of 1. Thus, when you first call queue.defer, it starts your first longRunningTask. However, the second call to queue.defer does not start the second task because the first task is still running, and 2 > 1. Your tasks are not abortable, so once they start, there is no way to cancel them.

When you then abort the queue, the second task is still not started, but the await callback is immediately invoked with an error. (Per the queue.abort documentation: “invokes the queue.await or queue.awaitAll callback with an error indicating that the queue was aborted.”) You’re ignoring this error, but if you only want to do something if your tasks complete (successfully), then you need to check for the error instead, like this:

q.await(function(error) {
  if (error) throw error;
  console.log("all done");
});

Then you’d see this output:

Error: abort
    at Object.q.abort (…/node_modules/d3-queue/build/d3-queue.js:90:34)
    at null._onTimeout (repl:1:27)
    at Timer.listOnTimeout (timers.js:92:15)
task finished

Again, the only way to avoid the “task finished” message is to make your tasks abortable, and to abort the queue before they finish; otherwise there is no way of aborting them, short of terminating the entire process.

If you want to “abort” the queue in the sense of letting any running tasks finish without letting any further pending tasks start, well, that’s not what queue.abort does. But I suppose you could set a sentinel value that your longRunningTask could check on start, and they could immediately invoke the callback with a null result rather than doing any actual work.

from d3-queue.

mbostock avatar mbostock commented on May 18, 2024

I’ve expanded the documentation for queue.abort.

I could maybe imagine a queue.cancel method which lets running tasks complete but doesn’t allow pending tasks to start. Something like this:

cancel: function() {
  if (error == null) waiting = NaN;
  return q;
}

But I haven’t tested it.

from d3-queue.

markrian avatar markrian commented on May 18, 2024

That cancel method sounds like what I'm after. Thanks for considering it!

from d3-queue.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.