16
var promise1 = new Promise(function(resolve, reject) { 
       // ajax api call
});
var promise2 = new Promise(function(resolve, reject) { 
       // ajax api call
});

I want to be able to do something like -

if(a < b) {
  promise1.cancel();
}
  • 9
    Promises are not cancellable. [The proposal](https://github.com/tc39/proposal-cancelable-promises) to add this functionality was withdrawn because of opposition from Google. – Felix Kling Jan 10 '17 at 07:13
  • Take a look at this question - http://stackoverflow.com/questions/29478751/how-to-cancel-an-emcascript6-vanilla-javascript-promise-chain – Ajay Narain Mathur Jan 10 '17 at 07:15
  • Take a look here : http://stackoverflow.com/questions/30233302/promise-is-it-possible-to-force-cancel-a-promise – Mihai Alexandru-Ionut Jan 10 '17 at 07:16
  • Promises are not cancellable refer to this [Link](http://stackoverflow.com/questions/30233302/promise-is-it-possible-to-force-cancel-a-promise) – Saurabh Agrawal Jan 10 '17 at 07:18
  • There are still discussions around this though, you might want to search on https://esdiscuss.org/ . – Felix Kling Jan 10 '17 at 07:21
  • Perhaps describe what problem you're really trying to solve and folks here could help you solve it with a supported set of functionality. – jfriend00 Jan 10 '17 at 10:15

1 Answers1

8

You can't cancel a Promise, but you can chain your promises with then, and reject at any point.

new Promise((resolve, reject) => {
    // step 1
})
.then((result) => {
    if (!result) {
        // Reject the promise chain
        throw 'cancel';
    } else {
        return ... // step 2
    }
})
.catch(err => {
    // cancelled
});
alekop
  • 2,838
  • 2
  • 29
  • 47