You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 PromiseasyncfunctionrequestAll(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 failedconstcancelTokenSource=newCancelTokenSource();// Promise.all would only call the given cancelTokenSource's// .cancel() if one of the request(...) throws an error, otherwise// it just proceeds as per normalconstresponses=awaitPromise.all(urls.map(url=>request(url,{cancelToken: cancelTokenSource.token}),cancelTokenSource,);returnresponses;}asyncfunctionrequestRace(urls){// NOTE: Again this has nothing to do with cancelling the returned// promise from Promise.raceconstcancelTokenSource=newCancelTokenSource();// 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 observedconstfirstResponse=awaitPromise.all(urls.map(url=>request(url,{cancelToken: cancelTokenSource.token}),cancelTokenSource,);returnfirstResponse;
The text was updated successfully, but these errors were encountered:
Regardless of whether it's a protocol or a distinct type or whatever, it would be nice if
Promise.race
andPromise.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):
The text was updated successfully, but these errors were encountered: