2

Isn't it possible to cancel an ES6 Promise by just calling Promise.reject(reason)?

Something like this:

Promise.resolve().then(function () {

    return 'foo';

}).then(function () {

    return console.log('we were here');

}).then(function () {

    return Promise.reject({type: 'canceled'})

}).then(function () {

    return console.log('never visited');

}).catch(function (e) {
    console.log(e);
});

is there any downside to "cancelling" promises via Promise.reject?

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • 2
    It's not a "cancellation" it is a "rejection". And `Promise.reject` was designed specifically to produce a rejected promise. – zerkms Jun 14 '16 at 22:52
  • Yes, but in the nearest catch block you can now decide what to do, and you achieve the goal of skipping sections of code. – Alexander Mills Jun 14 '16 at 22:56
  • Yes, you can. But it's not a "cancellation" :-) – zerkms Jun 14 '16 at 22:57
  • 3
    What is a cancellation, in your eyes? :) – Alexander Mills Jun 14 '16 at 22:57
  • I don't know, it's you who brought the term. The standard does not define it so I have no idea what you mean by that :-) – zerkms Jun 14 '16 at 22:59
  • well you seem to have an idea of what it means :) – Alexander Mills Jun 14 '16 at 23:00
  • 1
    No I don't. I personally prefer to use the well established terms not make ones randomly. – zerkms Jun 14 '16 at 23:01
  • 1
    @AlexMills [cancellation](https://en.wiktionary.org/wiki/cancel) has a well-defined meaning. I wouldn't apply it to what you are doing here - you're just throwing an exception at a certain point in your flow to break the promise chain. – Bergi Jun 15 '16 at 03:00
  • @Bergi ok but what would cancel mean to you in the context of a promise, (if it's not to jump to the nearest catch block?) Perhaps jump to the final catch block...? – Alexander Mills Jun 15 '16 at 20:24
  • @AlexMills: Cancelling the execution of a task usually means aborting it, jumping to a `finally` block if at all. ES6 Promises [don't support](http://stackoverflow.com/a/29479435/1048572) anything like that for now, however. – Bergi Jun 15 '16 at 21:07

2 Answers2

2

No, you can't cancel a promise with Promise.reject().

Promise.reject() is just a helper function that returns a new already rejected promise.

Case in point, if you already have a promise p, then there is no way to cancel it:

var p = fetch("flower.png");
// No way to reject p or "cancel" the fetch.

Importantly, p is not a control surface for the fetch. All you can do is reject subsequently chained promises:

p = p.then(result => cancel? Promise.reject(new Error("Cancelled")) : result);

or simply ignore p, which has the same effect.

That said, your code looks fine, but doesn't match your question. Most people think of this as error handling, or rejection propagation, not cancellation, though it does break the chain of subsequent asynchronous operations, as you show.

jib
  • 40,579
  • 17
  • 100
  • 158
  • 1
    "All you can do is reject subsequently chained promises" --- alternatively you may not wait and use `Promise.race()` (that way it would be immediate) – zerkms Jun 15 '16 at 03:53
0

There is no downside, it's just one of the ways you can handle a promise if it can't be fufilled

See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject

Matt Urtnowski
  • 2,556
  • 1
  • 18
  • 36