alkemics/CancelablePromise

Saving the CancelablePromise to variable

Closed this issue · 2 comments

I'm not sure if this is intentional or a bug, but here it is...

When I first started using this library I was saving my cancelable promises as such:

import CancelablePromise from 'cancelable-promise'
import Fetch from 'whatwg-fetch'

const getBlah = params => new CancelablePromise((resolve) => resolve(Fetch(...)))

let activeSearch = getBlah(...).then(response => ...)

In this case my activeSearch variable is still equal to an instance of CancelablePromise but calling activeSearch.cancel() before the fetch has resolved does not cancel the .then(response => ...) execution. After some frustration I tried something closer to what the README suggests:

let activeSearch = getBlah(...)

activeSearch.then(response => ...);

In this case calling activeSearch.cancel() does prevent execution of the pending .then().

It seems to me that any pending .then statements should be aborted no matter how you save your variable. However, I'm no expert with promises so not sure if this necessarily makes sense or is even possible given the current spec/polyfill.

In your example, you store the second CancelablePromise returned by the then. A then returns a new (Cancelable)Promise, it's in the promise's spec, so everything seems fine :)

Yeah now that I look at this again, I think the current behavior does make sense so I'll close.

Any idea if they've made any progress with pushing cancelability/aborting forward in the fetch spec? I've been watching some of the discussions but it still seems to be a little ways off.