21
loading...
This website collects cookies to deliver better user experience
TL;DR: We do not cancel Promise but operations. Cancellation is orthogonal to the abstraction of Promise. They have different concerns.
.cancel()
method!const promise = fetch('https://somedata.com').then(x=>x.json());
//... at some point
promise.cancel();
CancelledError
).cancel()
applied, which promise on the chain is really cancelled? Top(then "downstream" get notified by catching CancelledError
and re-throwing it)
But what if a part of Promises on the chain have resolved? A settled Promise can't modify its state, therefore it can't "notify" its "downstream" again.
If "upstream" is PENDING, what about other "downstream"s?
const root = fetch('https://somedata.com');
const promise1 = root.then(x=>x.json());
const promise2 = root.then(x=>x.string());
// ...at some point, root is PENDING
promise1.cancel(); // what about promise2?
Current (but don't care about "upstream")
Hmmm, it is
.ignore()
rather than.cancel()
. Not really what we expected.
Current (and cancel "upstream", if possible, i.e. no other "downstream" waiting for a value)
This intuitively makes better sense. But things get really complicated. This is to say explicit dependencies between CancellablePromise need to be managed.....
Actually the two made-up terms "upstream" and "downstream" do not exist in Promise/A+ spec at all. A Promise has no clue how it will be fulfilled/rejected.
Maybe RxJS has done this job.
// an example of aborting ongoing fetch
const controller = new AbortController();
const signal = controller.signal;
fetch('https://example.com', { signal });
// ...at some point
controller.abort();