machty / ember-concurrency

ember-concurrency is an Ember Addon that enables you to write concise, worry-free, cancelable, restartable, asynchronous tasks.
http://ember-concurrency.com
MIT License
691 stars 157 forks source link

v2: Make cancel & cancelAll awaitable #396

Closed maxfierke closed 3 years ago

maxfierke commented 3 years ago

Cancelation is async, but cancel and cancelAll have not returned promises to enable awaiting cancelation. This PR changes that so the two can be used with async/await and/or Promises to ensure consuming applications can reliably schedule subsequent operations that may depend on cancelation

For example,

@dropTask *myTask() {
  while(1) {
   console.log("hello!");
   yield timeout(1000);
  }
}

@task *restartMyTask() {
  // Note the `yield`. This could also be an `await` or `then` outside of tasks
  yield this.myTask.cancelAll();

  // Without being able to `yield` on `cancelAll` above, the cancelation wouldn't be guaranteed to have taken place before this is called, resulting in `perform` no-oping and dropping the task and myTask would not restart
  this.myTask.perform();
}