sindresorhus / p-queue

Promise queue with concurrency control
MIT License
3.35k stars 180 forks source link

Is it feasible to change priority of a Queued item on the fly #208

Open Raishav opened 2 weeks ago

Raishav commented 2 weeks ago

I'm working on a custom loader with asynchronous load promises, I want to change the priority of a specific promise function, can this be achieved using current code?

I managed to achieve something by queuing up the promise function again with updated priority, but this loads the item twice.

prioritize(key: string) {
    const promise = this.taskList.get(key);
    if (!promise) {
      console.error("No such promise: ", key);
    } else {
      this.pause();
      this.queue.add(promise, { priority: 2 });
      this.resume();
    }
}

Thanks.

sindresorhus commented 2 weeks ago

There could maybe be a .setPriority(fn, {priority}) method that could accept the original function you passed to .add() (for ID purposes) and let you change the priority.