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

Specify a builtin cancellation state #25

Open
Jamesernator opened this issue Jul 15, 2019 · 0 comments
Open

Specify a builtin cancellation state #25

Jamesernator opened this issue Jul 15, 2019 · 0 comments

Comments

@Jamesernator
Copy link

Currently in the current proposal there is no specific course of action that should be taken when cancellation is actually hit.

This causes a lot of boilerplate to deal with if every function has its own specific cancellation error especially when dealing with 'unhandledrejection' and the like.

For example consider an existing example of fetching a resource:

async function fetchThingTimeoutAfter5Seconds() {
  const abortController = new AbortController()
  return Promise.race([
    fetch('./some-url.ext', { signal: abortController.signal })
      // Ignore cancellations 
      .catch(err => if (!(err instanceof DOMException && err.name === 'AbortError')) throw err),
    new Promise((resolve, reject) => setTimeout(() => reject(new TimeoutError('')), 5000)
  ])
}

It's already quite cumbersome to add .catch onto any fetch just to filter out the cancellation error that we don't care about.

One alternative is to do something like return null in case of cancellation e.g.:

function delay<T>(time: number value: T, cancelSignal: CancelSignal=never) {
  return new Promise((resolve, reject) => {
    const timeout = setTimeout(resolve, time, value)
    cancelSignal.subscribe(() => {
      clearTimeout(timeout)
      resolve(null)
    })
  })
}

However this is painful when using tooling like TypeScript as it changes the return type just to accomodate for a value that can never be used.


Now the previous proposal had a third dedicated "cancelled" state that could propagate up functions like exceptions.

This was quite useful as it meant you could distinguish cancellation easily and could just ignore it if you didn't care about it.

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

1 participant