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

Integration with Promise.{all, race} #21

Closed
Jamesernator opened this issue Apr 19, 2018 · 3 comments
Closed

Integration with Promise.{all, race} #21

Jamesernator opened this issue Apr 19, 2018 · 3 comments

Comments

@Jamesernator
Copy link

Jamesernator commented Apr 19, 2018

Regardless of whether it's a protocol or a distinct type or whatever, it would be nice if Promise.race and Promise.all accepted an optional second argument that could be used to cancel the Promises that have not yet completed.

Here's a couple examples using an API roughly like the old CancelToken proposal but that's not important (if it were to use a protocol instead it would probably just be a callback or something like that):

// Assuming a request() function exists that returns a Promise
async function requestAll(urls) {
  // NOTE: This has nothing to do with cancelling the
  // returned Promise from Promise.all, it's simply to cancel other
  // requests that are no longer needed as a single one failed
  const cancelTokenSource = new CancelTokenSource();
 
  // Promise.all would only call the given cancelTokenSource's
  // .cancel() if one of the request(...) throws an error, otherwise
  // it just proceeds as per normal
  const responses = await Promise.all(
    urls.map(url => request(url, { cancelToken: cancelTokenSource.token }),
    cancelTokenSource,
  );
  return responses;
}


async function requestRace(urls) {
  // NOTE: Again this has nothing to do with cancelling the returned
  // promise from Promise.race
  const cancelTokenSource = new CancelTokenSource();
  
  // Promise.race would call the given cancelTokenSource's .cancel on the first
  // resolve/reject so that the other tasks cancel themselves rather
  // than continue to use resources for a result that will never be observed
  const firstResponse = await Promise.all(
    urls.map(url => request(url, { cancelToken: cancelTokenSource.token }),
    cancelTokenSource,
  );
  return firstResponse;
@rbuckton
Copy link
Collaborator

rbuckton commented Jul 17, 2018

@Jamesernator could you not do this just as easily via Promise.prototype.finally()?:

await Promise.all(urls.map(url => request(url, { cancelToken: cancelTokenSource.token }))
  .finally(() => cancelTokenSource.cancel());

@Jamesernator
Copy link
Author

Yeah I didn't actually think of that, I'll close this for now unless I think of a case that wouldn't work with that.

@rbuckton
Copy link
Collaborator

If you look at the bottom of #22, I am considering how cancellation could interop with the Promise API, so I will keep this in mind for the future.

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

2 participants